I have a problem with random tool animation

Hello ! My task is to make a tool that will make random animation , I tried make it but nothing . Maybe you’ll help me ? Thank you ! Here a script from a tool that will play random punch animation .

local Debounce = false
local animations = {script.Parent.Animation1 , script.Parent.Animation2}

script.Parent.Equipped:Connect(function(Mouse)
	animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(math.random(1,animations))
	Mouse.Button1Down:Connect(function()
		if Debounce == false then
			Debounce = true
		    animation:Play()
			wait(0.7)
			animation:Stop()
			wait(0.1)
			Debounce = false
		end
    end)
end)

script.Parent.Unequipped:Connect(function()
	animation:Stop()
end)

One simple mistake I noticed is here.

number math.random ( number m = 0.0, number n = 1.0 )

“Animations” is an array, when math.random requires 2 numbers. Hence you can use the # operator to find the arrays size which would look like (math.random(1, #animations)).

Next, at least with you got everything set up, another fix would be to put this line into the Button1Down event function. Otherwise, once you equip the weapon, a random animation would be chosen and then only that one would be played until unequipped and then reequipped chosen another single animation to play.

1 Like

Do you mean I need to change it to this ?

local Debounce = false
local animations = {script.Parent.Animation1 , script.Parent.Animation2}

script.Parent.Equipped:Connect(function(Mouse)
	animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(math.random(1,#animations))
	Mouse.Button1Down:Connect(function(animation)
		if Debounce == false then
			Debounce = true
		    animation:Play()
			wait(0.7)
			animation:Stop()
			wait(0.1)
			Debounce = false
		end
    end)
end)

script.Parent.Unequipped:Connect(function()
	animation:Stop()
end)

Well that’s one fix, but you also should move this entire line down into the nested function.

Actually though, putting it into the if statement with the denounce would be best. So directly after this line should be fine.

1 Like

Sorry , I just don’t understand . Can you change the piece of code that I did wrong ? Thanks !

As I was saying the line that selects the random animation should be after Debounce = true. This should fix everything that I noticed atleast.

1 Like

I’m pretty sure there is a tutorial about this topic! Make sure to research before posting.How to animate Tool Parts (Guns, Knifes etc.) (The tutorial) Hope this helped!

Oh Sorry, somehow overlooked a big issue.
animation should be equal to

animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animations[math.random(1,#animations)])

You were initially just setting animation to a random value not an actual animation

1 Like

I have a problem with it , I made it like this and it doesn’t work still . Do I missed something ?

local Debounce = false
local animations = {script.Parent.Animation1 , script.Parent.Animation2}

script.Parent.Equipped:Connect(function(Mouse)
	
	Mouse.Button1Down:Connect(function()
		if Debounce == false then
			Debounce = true
            animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(math.random(1,animations))
		    animation:Play()
			wait(0.7)
			animation:Stop()
			wait(0.1)
			Debounce = false
		end
    end)
end)

script.Parent.Unequipped:Connect(function()
	animation:Stop()
end)
local animations = {script.Parent:WaitForChild("Animation1") , script.Parent:WaitForChild("Animation1")}

Had to change to this before it would run on my end. On the client, scripts tend to run before many items load, so a WaitForChild is often needed.

Here is my entire script which does work on my end.

local Debounce = false
local animations = {script.Parent:WaitForChild("Animation1") , script.Parent:WaitForChild("Animation2")}

script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		if Debounce == false then
			Debounce = true
            animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animations[math.random(1,#animations)])
		    animation:Play()
			wait(0.7)
			animation:Stop()
			wait(0.1)
			Debounce = false
		end
    end)
end)

script.Parent.Unequipped:Connect(function()
	animation:Stop()
end)

Also, It should be in a local script to detect the users input, also the animations will replicate.
If you still have any errors it might be with the animation themselves. Otherwise, this code works for me.

Hope this helps. :+1:

1 Like