What's wrong with my script?

  1. What do you want to achieve?
    When the proximity prompt is triggered, player2 syncs their animation to player1. What I would like to add to the script is if the prompt is triggered and player2 is dancing, then the animation for player2 will stop/return back to normal.

  2. What is the issue?
    Everything is working besides the stop animation feature. The prints are printing correctly, but the player2 animation won’t stop.

I’m quite new to scripting so any help is appreciated!!

Video:

LocalScript (inside of PlayerStarterScripts):

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local remoteStopEvent = ReplicatedStorage:WaitForChild("RemoteStopEvent")

local function onClientEvent(player1, player2)
	print("Client Event Triggered")
	if player1.Character then
		local track = player1.Character.Humanoid.Animator:GetPlayingAnimationTracks()[1]

		if track and player2.Character then
			print("Playing Animation Track on Client")
			local timeStamp = track.TimePosition :: number
			local track = player2.Character.Humanoid.Animator:LoadAnimation(track.Animation)
			track:Play()
			track.TimePosition = timeStamp

		end
	end
end

local function onClientStopEvent(player1, player2)
	print("Client Stop Event Triggered")

	if player2.Character then
		local humanoid2 = player2.Character:FindFirstChild("Humanoid")

		if humanoid2 and humanoid2:IsA("Humanoid") then
			local animationTracks2 = humanoid2:FindFirstChildOfClass("Animator"):GetPlayingAnimationTracks()

			-- Stop all playing animation tracks on player2
			for _, track in pairs(animationTracks2) do
				track:Stop()
			end
		end
	end
end

remoteEvent.OnClientEvent:Connect(onClientEvent)
remoteStopEvent.OnClientStopEvent:Connect(onClientStopEvent)
print("Client Script Loaded")

Serverscript (inside of ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local remoteEvent = ReplicatedStorage.RemoteEvent
local remoteStopEvent = ReplicatedStorage.RemoteStopEvent

local playing = false

local function onTriggered(player1, player2)
	print("Triggered on Server")

	remoteEvent:FireAllClients(player1, player2)
	playing = true
	print (playing)
end

local function stopAnimation(player1, player2)
	print("Triggered on Server")

	remoteStopEvent:FireAllClients(player1, player2)
	playing = false
	print (playing)
end

local function onCharacterAdded(character, player1)
	print("Character Added")
	local ownerPrompt = Instance.new("ProximityPrompt")
	ownerPrompt.ActionText = "Join"
	ownerPrompt.Name = "OwnerPrompt"
	ownerPrompt.HoldDuration = 1
	ownerPrompt.ObjectText = "Hold E to join dance!"
	ownerPrompt.RequiresLineOfSight = false
	ownerPrompt.Parent = character.PrimaryPart

	ownerPrompt.Triggered:Connect(function(player2)
		print("Prompt Triggered")
	if playing == false then
			onTriggered(player1, player2)
		else if playing == true then
				stopAnimation(player1, player2)
				
			end
		end
		
	end)
end

local function onPlayerAdded(player)
	print("Player Added")
	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
print("Server Script Loaded")

& 2 REs in Replicated storage (named RemoteEvent & RemoteStopEvent)!

4 Likes

Isn’t it “OnClientEvent” instead of “OnClientStopEvent”?

maybe it’s not this and maybe I’m seeing something wrong. Is this on purpose or was it a mistake? :thinking:

3 Likes

Thank you so much for the reply!!! <3 I have two events that I’m trying to connect & fire:

The event that syncs the animation:

And the event that stops the animation:

Which are connected to the Prompt Triggered:

Maybe I’m not doing it right? I’m quite new to scripting so I don’t know :confused:

5 Likes

I will try to read your code well to be able to help you! I will get back to you in a while :slight_smile:

3 Likes

Thank you so so much!!! <3 <3 you are the best!

3 Likes

Hi, me again, I’m reviewing your code and so far everything is working, I’m trying to refactor it and fix it so you don’t get bugs in the future. As soon as I finish it I will pass it to you and you tell me if it works for you :happy1:

3 Likes

Oh my goodness thank you!! That would be extremely helpful!

2 Likes

omg i hate roblox bugs, it gives me error for some reason, i will try to fix it fast, sorry for the delay D:

3 Likes

Yeah, it should be OnClientEvent, it’s weird if that didn’t error lol

3 Likes

Did you try printing all the playing tracks in the Player2?

4 Likes

I’m trying to figure out what’s going on, I tried to fix the code but it’s still the same, it’s really weird…

3 Likes

Hm, hold on, let me do some research rq lol

2 Likes

I think she will have to stop the animation manually, otherwise it won’t work :skull:

Probably, try to print the animation tracks tho.

they do come out but do not stop when moving

Oh, hold on, maybe we’re being dumb here, the function that stops the animation is connected to the remoteStopEvent RemoteEvent which connects incorrectly the OnClientEvent event, fix that and try again.

Nvm, if the prints come out then that’s not the issue.

this is so weird lol idk why this happens

1 Like

Hold on, call the GetPlayingAnimationTracks function from the Humanoid instead of the Animator.

Hm, it seems like the function in the Humanoid is deprecated, probably not a good idea.

1 Like