The game I’m currently making requires animations be called from the server to play to the client. It does this by calling PlayAnimationReplicate, where it then loops through all players and plays the animation for them. Simple, right?
function functionModule.PlayAnimationReplicate(char, animName, toPlay)
local plr = playerService:GetPlayerFromCharacter(char)
local remotesFolder = char.Events.Remote
remotesFolder.PlayAnimation:FireClient(plr, animName, toPlay) -- all this does is just play the animation on the plr client
for i, otherPlr in pairs(playerService:GetPlayers()) do
if otherPlr ~= plr then
local otherChar = otherPlr.Character
if otherChar then
print(otherChar)
otherChar.Events.Remote.ReplicateAnim:FireClient(otherPlr, char, char.Weapon.Anims[animName], toPlay)
end
end
end
end
remotesFolder.ReplicateAnim.OnClientEvent:Connect(function(charToAnimate, animInstance, toPlay)
local animator = charToAnimate.Humanoid:WaitForChild("Animator")
if not loadedAnims[charToAnimate] then
loadedAnims[charToAnimate] = {}
end
print("Received! "..animInstance.Name.." "..tostring(toPlay).." "..tostring(animInstance.AnimationId))
local anim = loadedAnims[charToAnimate][animInstance.Name]
if toPlay then
if not anim then
loadedAnims[charToAnimate][animInstance.Name] = animator:LoadAnimation(animInstance)
anim = loadedAnims[charToAnimate][animInstance.Name]
anim:Play()
print("Playing new")
else
print("playing previously made")
anim:Play()
end
local event
event = animator.AnimationPlayed:Connect(function(animTrack)
--print(1, animTrack.Animation.AnimationId, anim.Animation.AnimationId)
if animTrack.Name == "Animation" and animTrack.Animation.AnimationId == anim.Animation.AnimationId then
print("Cancel the auto-replication from the charToAnimate")
animTrack:Stop(0)
end
end)
wait(anim.Length)
if event then
event:Disconnect()
end
else
print("Stopping")
if anim then
anim:Stop()
end
end
end)
The problem comes along when the animation is played for the first time, meaning it prints “Playing new.” It plays just fine, but the AnimationPlayed event doesn’t pick up auto-replicated animation from charToAnimate’s client, meaning that the animation plays twice. Every other time where it plays the already-made animation, it works just fine.
Is there anyway to fix this? Or will I have to redesign everything from scratch.