How do I play an animation infinitely

How do I play an animation forever until there is a bomb.
When a player joins, they get a bomb and the animation plays like holding the animation

But the animation plays for 1 time and then stops playing.

local ss = game:GetService("ServerStorage")

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		task.wait(5)
		local clone = ss.Bomb:Clone()
		clone.Parent = char
		clone.CFrame = char.HumanoidRootPart.CFrame + char.HumanoidRootPart.CFrame.LookVector*2.5
		local weld = Instance.new("WeldConstraint",clone)
		weld.Part0 = clone
		weld.Part1 = char.HumanoidRootPart
		local anim = char.Humanoid.Animator:LoadAnimation(workspace.Animation)
		anim.Looped = true
		anim:Play()
	end)
end)

How do I play the anim until I dont hold the bomb anymore

2 Likes

One way to achieve this is to use a while loop to constantly check whether the player is holding the bomb, and to play the animation for as long as they are holding it. So, it’ll be like:

local ss = game:GetService("ServerStorage")
local anim = workspace.Animation

game.Players.PlayerAdded:Connect(function(plr)
    local holdingBomb = false
    
    plr.CharacterAdded:Connect(function(char)
        task.wait(5)
        local clone = ss.Bomb:Clone()
        clone.Parent = char
        clone.CFrame = char.HumanoidRootPart.CFrame + char.HumanoidRootPart.CFrame.LookVector*2.5
        local weld = Instance.new("WeldConstraint", clone)
        weld.Part0 = clone
        weld.Part1 = char.HumanoidRootPart
        
        while holdingBomb do
            anim.Looped = true
            anim:Play()
            task.wait()
        end
        
        anim:Stop()
    end)
    
    plr.CharacterRemoving:Connect(function(char)
        holdingBomb = false
        anim:Stop()
    end)
    
    plr.Character.ChildAdded:Connect(function(child)
        if child == ss.Bomb then
            holdingBomb = true
        end
    end)
    
    plr.Character.ChildRemoved:Connect(function(child)
        if child == ss.Bomb then
            holdingBomb = false
        end
    end)
end)

In case you don’t understand, the holdingBomb variable is used to keep track of whether the player is holding the bomb or not. The while loop is used to continuously play the animation as long as holdingBomb is true.

The plr.CharacterRemoving event is used to stop the animation when the player’s character is removed, and the plr.Character.ChildAdded and plr.Character.ChildRemoved events are used to update the holdingBomb variable whenever the player picks up or drops the bomb.

1 Like

Try this :slight_smile:

local ss = game:GetService("ServerStorage")

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        task.wait(5)
        local clone = ss.Bomb:Clone()
        clone.Parent = char
        clone.CFrame = char.HumanoidRootPart.CFrame + char.HumanoidRootPart.CFrame.LookVector * 2.5
        local weld = Instance.new("WeldConstraint", clone)
        weld.Part0 = clone
        weld.Part1 = char.HumanoidRootPart
        local anim = char.Humanoid.Animator:LoadAnimation(workspace.Animation)
        anim.Looped = true
        anim:Play()

        while clone:IsDescendantOf(char) do
            task.wait() -- Wait for a short duration before checking again

            -- Check if the bomb is still present
            if not clone:IsDescendantOf(char) then
                anim:Stop() -- Stop the animation if the bomb is removed
                break -- Exit the loop
            end
        end
    end)
end)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.