-- Update the cutscene every render frame

RunService.RenderStepped:Connect(function(Delta)
	Demo:Update(Delta)
end)

if PlayOnPlayerJoin and PlayOnPlayerJoin.Value then
	-- Play the cutscene
	PlayCutscene()
end

if PlayOnPartTouch then
	local part = script.PlayOnPartTouch.Value
	if part and part:IsA("BasePart") then
		part.Touched:Connect(function(hit)
			if hit.Parent == Player.Character then
				PlayCutscene()
			end
		end)
	end
	PlayOnPartTouch:Destroy()
end

if PlayOnEventFire then
	local e = PlayOnEventFire.Value
	if e and e:IsA("BindableEvent") then
		e.Event:Connect(PlayCutscene)
	end
	PlayOnEventFire:Destroy()
end

if PlayOnRemoteEventFire then
	local e = PlayOnRemoteEventFire.Value
	if e and e:IsA("RemoteEvent") then
		e.OnClientEvent:Connect(PlayCutscene)
	end
	PlayOnRemoteEventFire:Destroy()
end

if StopOnRemoteEventFire then
	local e = StopOnRemoteEventFire.Value
	if e and e:IsA("RemoteEvent") then
		e.OnClientEvent:Connect(function()
			Demo:Stop()
		end)
	end
	StopOnRemoteEventFire:Destroy()
end

if StopOnRemoteEventFire then
	local e = StopOnRemoteEventFire.Value
	if e and e:IsA("RemoteEvent") then
		e.OnClientEvent:Connect(function()
			Demo:Stop()
		end)
	end
	StopOnRemoteEventFire:Destroy()
end

if CutsceneGui then
	CutsceneGui.Parent = Player:WaitForChild("PlayerGui")
	local SkipCutsceneButton = CutsceneGui:FindFirstChild("SkipCutscene", true)
	local PlayCutsceneButton = CutsceneGui:FindFirstChild("PlayCutscene", true)
	
	if SkipCutsceneButton and SkipCutsceneButton:IsA("GuiButton") then
		SkipCutsceneButton.MouseButton1Click:Connect(function()
			Demo:Stop()
		end)
	end
	
	if PlayCutsceneButton and PlayCutsceneButton:IsA("GuiButton") then
		PlayCutsceneButton.MouseButton1Click:Connect(function()
			PlayCutscene()
		end)
	end
	Demo.EStop.Event:Connect(function()
		CutsceneGui.Enabled = false
	end)
end

local Character = Player.Character or Player.CharacterAdded:Wait()

if PlayOnCharacterAdded and PlayOnCharacterAdded.Value then
	PlayCutscene()
end
if PlayOnCharacterDied and PlayOnCharacterDied.Value then
	local Humanoid = Character:WaitForChild("Humanoid")
	Character:WaitForChild("Humanoid").Died:Connect(function()
		PlayCutscene()
	end)
end
PlayOnCharacterAdded:Destroy()
