Hi, I want to make a police radio animation so when you click a button it moves your hand and puts your hand down when you click the button again. Here is my current code:
-- Server Script
RadioStartAnimationRemote.OnServerEvent:Connect(function(Player)
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid:LoadAnimation(script.RadioAnimation):Play()
end)
RadioEndAnimationRemote.OnServerEvent:Connect(function(Player)
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid:LoadAnimation(script.RadioAnimation):Stop()
end)
You can play the animation from the client and it will still replicate, no need to do it from the server. Also, use Humanoid.Animator as using Humanoid for loading animations is deprecated.
Also, it doesn’t stop the animation because when you do LoadAnimation again, it just makes another AnimationTrack, it doesn’t reference the old one. Your solution, after making it client sided instead, would be to store the result of Humanoid:LoadAnimation(script.RadioAnimation) in a variable to use for playing and stopping
That would be a reasonable action for concern, I think it’s playing late because it has to tell the server to play it which would be slower than doing it immediately from the client.
You’d probably need to have a dictionary set up to contain all AnimationTracks for the radio for each player then, and reference it when you need to stop it
You need to store the animation track you created to stop it. The delay is probably from the communication time between the server and the client or (possibly) from your animation.
local animationTrack
RadioStartAnimationRemote.OnServerEvent:Connect(function(Player)
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid", 5)
if not Humanoid then
return
end
if animationTrack then
animationTrack:Destroy()
end
animationTrack = Humanoid:LoadAnimation(script.RadioAnimation)
animationTrack:Play()
end)
RadioEndAnimationRemote.OnServerEvent:Connect(function(Player)
if animationTrack then
animationTrack:Stop()
end
end)
That is not a good way to go about it if another player fires to the server, because you’re using a variable that will only store one at a time and overwrite if there was another which would cause others to still play the animation, a dictionary set up is needed
local animationTracks = {}
RadioStartAnimationRemote.OnServerEvent:Connect(function(player)
local character = player.Character
local humanoid = character and character:FindFirstChild("Humanoid")
if not humanoid then
return
end
animationTracks[player] = Humanoid.Animator:LoadAnimation(script.RadioAnimation)
animationTracks[player]:Play()
end)
RadioEndAnimationRemote.OnServerEvent:Connect(function(player)
local track = animationTracks[player]
if not track then
return
end
track:Stop()
track:Destroy()
animationTracks[player] = nil
end)
Yes but if you want to do it like that to prevent people changing the animation, then it should still work, but again iit’s mostly likely the reason why you’re having it start late, if you want to fix that, you probably need to play it locally instead of telling the server to do that