Animation Not Stopping After Loop

The problem with this code is that the animation only stops on one person’s character, and continues playing for everyone else. I’m not sure why it’s happening though. If anyone could help with this it would be appreciated.

				for i,v in pairs(game.Players:GetChildren()) do
				if v:IsA("Player") then
					if game.Workspace:FindFirstChild(v.Name) then
						local char = v.Character
						if char:FindFirstChild("Humanoid") then
							local hum = char:FindFirstChild("Humanoid")
							local anim = hum:LoadAnimation(game.ReplicatedStorage.Animation)

The animation stop statement is outside of the for loop so this won’t be ran for every player, rather only one. On that note, I’m not even sure how it stops for one player at all since the anim variable is local to the scope of the if statement it’s under.

There are many different ways I can think of regarding resolving this problem. If I were to be resolving this myself, I would simply have the server tell all clients via a RemoteEvent that they should be playing the clapping animation, since I prefer animations to be handled on the client.

Making this work for the server, my brain doesn’t want to do it. I can still provide you a way but it might not be as savory and it should probably be revisited in the future.

-- Even if you only reference them once, keeping services at the top of
-- your scripts helps for later updates and avoiding redundancy.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local animTracks = {}


-- ipairs and GetPlayers are canonical. GetPlayers will only return a table of
-- Player instances, making it unnecessary to class-check.
for _, player in ipairs(Players:GetPlayers()) do
    -- Use their Character property instead. Named items can cause edge cases
    -- where a username is the same as another child of the Workspace.
    local character = player.Character
    -- If character is non-nil, FindFirstChild for a Humanoid instance
    local humanoid = character and character:FindFirstChildOfClass("Humanoid")
    -- Need to check ancestry, otherwise script may throw an error
    if character and character:IsDescendantOf(workspace) and humanoid then
        local anim = humanoid:LoadAnimation(ReplicatedStorage.Animation)
        -- Insert the track into our table for later uses
        table.insert(animTracks, anim)


-- Remember that table earlier? Makes things easier.
for _, track in ipairs(animTracks) do
    -- May need to pcall if you ever encounter errors here, I'm assuming
    -- existence of instances in this table
    -- Always destroy instances you won't use again

you basically put anim:Stop() outside of the function so the variable doesn’t really
know what anim means because it’s out of the function.

Try to put it before the end
same for the wait, put it before end

sorry i didnt realize that above figured it out
but whats with the oversolution…

1 Like