I’ve been making a flashlight with the help of a friend, but I ran into one issue… The Flashlight Animation doesn’t play for everyone…
Now, the Script is Local, I understand that, but I tried making it a normal script (inside of the tool), won’t work though, for obvious reasons.
I tried tinkering around with the locals, but it only seems to break the animation.
I am utter garbage at scripting, how could I convert this to a normal script, to make the animation play for everyone in the server?
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local anim = hum:LoadAnimation(script.Parent.Wave)
--
script.Parent.Equipped:Connect(function()
anim:Play()
end)
script.Parent.Unequipped:Connect(function()
anim:Stop()
end)
Thank you for any tips/hints on how I could make this work for everyone.
PS: I’m guessing RemoteEvents have to be used in this case? No idea, as I said, I’m garbage at scripting…
Basically what I think is happening here is that you loaded the animation before the animator even replicated from the server to your client. When you don’t have the animator object, it won’t replicate to the server.
Both Humanoid:LoadAnimation() and AnimationController:LoadAnimation() will create an Animator if one does not already exist. When calling LoadAnimation from LocalScripts you need to be careful to wait for the Animator to replicate from the server before calling LoadAnimation if you want character animations to replicate. You can do this with WaitForChild(“Animator”).
You need to wait for an Animator descendant before you load the animation.
You can do it anywhere before the line that loads teh animation, you just need to have the humanoid. I’d put it right after you get the humanoid.
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
hum:WaitForChild("Animator")
local anim = hum:LoadAnimation(script.Parent.Wave)
--
script.Parent.Equipped:Connect(function()
anim:Play()
end)
script.Parent.Unequipped:Connect(function()
anim:Stop()
end)
Here’s how my flashlight animation script sort of looks like.
I generally think it looks cleaner and easier to read and modify.
And the animation shows for everyone.
local tool = script.Parent
local player = game.Player.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local IdleAnimation = Instance.new("Animation")
IdleAnimation.AnimationId = "rbxassetid://1234567890"
local IA = character:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(IdleAnimation)
tool.Equipped:Connect(function()
IA:Play()