How can I make an animation play the full length

I am trying to find the best way to play an animation on a humanoid when the player clicks, but make it so that animation plays the full length. I don’t want the animation to keep re-playing everytime a player clicks. If anyone has any ideas feel free to post it!

My code on the client:

local userinputservice = game:GetService("UserInputService")

userinputservice.InputBegan:Connect(function(input, gameProcessedEvent)
	local inputType = input.UserInputType
	if inputType == Enum.UserInputType.MouseButton1 then 
		if gameProcessedEvent == true then
		else
			game.ReplicatedStorage.Clicking:FireServer(plr)
		end
	end
end)

My code on the server:

game.ReplicatedStorage.Clicking.OnServerEvent:Connect(function(plr)
	local playerModel = game.Workspace:FindFirstChild(plr.Name)
	local humanoid = playerModel.Humanoid
	plr.leaderstats.Bucks.Value = plr.leaderstats.Bucks.Value + 1
	local FinalAnim = humanoid:LoadAnimation(game.Workspace.TossingMoney)
	FinalAnim:Play()
end)

You should only load the animation once for each character. When you have it loaded and have a reference to it, you can check if AnimationTrack.IsPlaying of that animationtrack is true. If it’s not, play it. You could store the animationtracks of each character into a table and when the character dies, remove them from the table and load new ones when the player respawns.

You can create a value called enabled and play the animation only if the value is true and make it false when the animation starts and set it back to true when animation finishes.

Here is a link to an event that fires when animations finish.

— Solution
Add a denounce that resets when the animation track receives the stopped event.

local denounce = false
game.ReplicatedStorage.Clicking.OnServerEvent:Connect(function(plr)
        if denounce then return end
        denounce = true
	local playerModel = game.Workspace:FindFirstChild(plr.Name)
	local humanoid = playerModel.Humanoid
	plr.leaderstats.Bucks.Value = plr.leaderstats.Bucks.Value + 1
	local FinalAnim = humanoid:LoadAnimation(game.Workspace.TossingMoney)
	FinalAnim:Play()
        FinalAnim.Stopped:Wait()
        denounce = false
end)

This is just an example. It was written on my phone so it’s formatted poorly and probably has typos. Good luck!

1 Like

Oh I see you use that for every player. In that case, make your denounce a table with player IDs as keys then have Booleans as values. Then use those values as denounces. Ex.

debounceTable = {}
Scriptsignal:Connect(function(player)
 local playerId = player.UserId
 if debounceTable[userId] then return end
 debounceTable[userId] = true

 [[code]]

 otherScriptSignal:Wait()
 debounceTable[userId] = nil [[ or false. ]]
end)

Again, written on my phone. I can’t write comments or indent stuff. Good luck with what your making!