Random animation debounce?

Okay so, how to make it that everytime you CLICK, it gives u a random animation BUT won’t do that when its debouncing. when its outside the function it only gives you one animation and doesn’t change it, but when i move the math random inside the function, it breaks the lower parts of the script unless i add a second mathrandom there. sorry this is hard to explain but i hope you understand what i’m trying to achieve.

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild('Humanoid')
local animationsFolder = game.Workspace.Randomani:GetChildren()

local randomAnimation = animationsFolder[math.random(1, #animationsFolder)]


On = false
script.Parent.MouseButton1Click:connect(function()
	if On == false then
		local playAnim = humanoid:LoadAnimation(randomAnimation)
		playAnim:Play()
		humanoid.WalkSpeed = 0
		On = true
		
	elseif On == true then
		local PlayingTracks = humanoid:GetPlayingAnimationTracks();
		for _, Track in ipairs (PlayingTracks) do
			if Track.Animation == randomAnimation then
				
				Track:Stop();
				randomAnimation:Destroy()
			end
		end
		
		humanoid.WalkSpeed = 16
		
		On = false
		
		
		end
	end)

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild('Humanoid')
local animationsFolder = game.Workspace.Randomani:GetChildren()

local CurrentPlayingAnimation = nil

On = false
script.Parent.MouseButton1Click:connect(function()
        local randomAnimation = animationsFolder[math.random(1, #animationsFolder)] -- generates a new anim
	if On == false then
		local playAnim = humanoid:LoadAnimation(randomAnimation)
		playAnim:Play()
		humanoid.WalkSpeed = 0
                 CurrentPlayingAnimation = randomAnimation
		On = true
		
	elseif On == true then
		local PlayingTracks = humanoid:GetPlayingAnimationTracks();
		for _, Track in ipairs (PlayingTracks) do
			if Track.Animation == CurrentPlayingAnimation then --checks current playing animation
				
				Track:Stop();
				randomAnimation:Destroy() -- i have 0 clue why youre destroying the animation, but up to you
			end
		end
		CurrentPlayingAnimation = nil
		humanoid.WalkSpeed = 16
		
		On = false
		
		
		end
	end)

Try this:

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild('Humanoid')
local animator = humanoid:WaitForChild("Animator")
local animationsFolder = game.Workspace.Randomani:GetChildren()

local randomAnimation

On = false

script.Parent.MouseButton1Click:Connect(function()
	
	if On == false then
		randomAnimation = animationsFolder[math.random(1, #animationsFolder)]
		local playAnim = animator:LoadAnimation(randomAnimation)
		playAnim:Play()
		humanoid.WalkSpeed = 0
		On = true

	elseif On == true then
		
		local PlayingTracks = animator:GetPlayingAnimationTracks();
		for _, Track in ipairs (PlayingTracks) do
			if Track.Animation == randomAnimation then

				Track:Stop();
				randomAnimation:Destroy()
			end
		end

		humanoid.WalkSpeed = 16

		On = false


	end
end)

yes thank you very much. this worked :smiley:

1 Like