Player Cutscene Bug

Hey guys,

A cutscene in my game is problematic because it has contrasting outcomes when it is presented to the player. The intended outcome is that a replication of the player’s avatar is cloned and sent into a set direction every time the cutscene is triggered, but this is not possible with my scripts. Because my game operates on a round-based system, after the first round (i.e., the second and subsequent rounds), the player’s avatar is not cloned, or may even be duplicated. Despite the changing outcomes, the print statements are able to be executed fine, so it is difficult to isolate the issue. I feel it is important to mention that the cutscene is cloned (and destroyed five seconds after the cutscene ends) before the subsequent round. See the media below for reference.

Intended Outcome that needs to be able to repeated:

Actual Outcomes:


image

Local Script:

local AgentCutsceneModel = nil
game.Workspace.ChildAdded:Connect(function(Child)
	if Child.Name == "AgentCutsceneModel" then
		AgentCutsceneModel = Child
		print(AgentCutsceneModel.Parent)
		
		local Start = game.Workspace.AgentCutsceneModel:WaitForChild("AgentCutscene - NPCStart")
		local Destination = game.Workspace.AgentCutsceneModel:WaitForChild("AgentCutscene - NPCDestination")
		
		game.ReplicatedStorage.AgentCutscene.onClientEvent:Connect(function()
			local Dummy = game.Players.LocalPlayer.Character
			Dummy.Archivable = true
			local ClonedDummy = Dummy:Clone()
			if not ClonedDummy:FindFirstChild("HumanoidRootPart") then
			repeat
				local Dummy = game.Players.LocalPlayer.Character
				ClonedDummy = Dummy:Clone()
				task.wait(0.1)
				until ClonedDummy:FindFirstChild("HumanoidRootPart")
			end
			
			Dummy.Archivable = false
			ClonedDummy.Parent = workspace
			local CurrentPivot = ClonedDummy:GetPivot()
			ClonedDummy:PivotTo(Start.CFrame + Vector3.new(0,5,0))
			print("dummy teleported to start of scene")
			
			local RunAnimation = ClonedDummy.Animate.run:WaitForChild("run")
			local IdleAnimation = ClonedDummy.Animate.idle:WaitForChild("Animation1")
			local RunAnimationTrack = ClonedDummy.Humanoid:LoadAnimation(RunAnimation)
			local IdleAnimationTrack = ClonedDummy.Humanoid:LoadAnimation(IdleAnimation)

			RunAnimationTrack:Play()
			ClonedDummy.Humanoid:MoveTo(Destination.Position)
			print("dummy moving to destination")
			ClonedDummy.Humanoid.MoveToFinished:Wait()
			print("dummy is waiting")
			IdleAnimationTrack:Play()
			
			task.wait(2)

			ClonedDummy:Destroy()
			print("dummy destroyed")
		end)
	end
end)

Global (Parts of the code block have been removed for clarity):

	local AgentCutscene = game.ReplicatedStorage.AgentCutscene
    local AgentCutsceneModel = game.ReplicatedStorage.Cutscenes:FindFirstChild("AgentCutsceneModel")

	for i, Player in pairs(game.Players:GetPlayers()) do
		if Player.Team == game.Teams.Agents then
			ClonedAgentCutsceneModel = AgentCutsceneModel:Clone()  
			ClonedAgentCutsceneModel.Parent = workspace
			AgentCutscene:FireClient(Player)

-- So on and so forth... 

A common mistake is having event connections within event connections (without disconnecting). What I mean by this is you have a childAdded connection → every time it runs you are also connecting a .OnClientEvent → this results in the OnClientEvent firing multiple times because you connected it multiple times. What I suggest you do is have the event separate (literally just move it out side the ChildAdded event). As for the “start” and “destination” variables → just use the agentCutsceneModel and get the start and end locations from there.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.