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.
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)!
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
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.