Playing animation not working

I am trying to make it when you have a tool, and activate it it plays a punching animation. But it doesn’t seem to play it, but no errors.

I have tried YouTube videos on how to play animations, didn’t work.

Here is my code, it’s in a server script inside the tool, with the animations in the script.
image

local leftPunch = script:WaitForChild("LeftPunch")
local rightPunch = script:WaitForChild("RightPunch")
local tool = script.Parent


local debounce = false
local waitTime = 0.4

script.Parent.Activated:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	
	local RightAnimTrack = Humanoid:LoadAnimation(rightPunch)
	local LeftAnimTrack = Humanoid:LoadAnimation(leftPunch)
	
	if debounce == false then
		debounce = true
		-----------------------
		local rn = math.random(1,2)

		if rn == 1 then
			RightAnimTrack:Play()
		elseif rn == 2 then
			LeftAnimTrack:Play()
		end
		-----------------------
		wait(waitTime)
		debounce = false
	end

end)
2 Likes

Did you upload the animation? Animations only play if they are owned by the owner of the game or the official Roblox account.

Yes I own them. I spend a while making them.

Do the animations have Action priority? Sometimes animations dont play because of low priority compared to the default animations.

Yep, they are both set on action.

Try using LoadAnimation on the Animator instead of the humanoid. If that doesn’t work then try using Print() where the animations are ran to see if they’re working

local RightAnimTrack = LoadAnimation(rightPunch)

so like this?

put Print(“Hello”) infront of where it loads and plays animations to see if one of them doesn’t get sent.

local leftPunch = script:WaitForChild("LeftPunch")
local rightPunch = script:WaitForChild("RightPunch")
local tool = script.Parent


local debounce = false
local waitTime = 0.4

script.Parent.Activated:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	
	local RightAnimTrack = Humanoid:LoadAnimation(rightPunch)
	local LeftAnimTrack = Humanoid:LoadAnimation(leftPunch)
	
	if debounce == false then
		debounce = true
		-----------------------
		local rn = math.random(1,2)

		if rn == 1 then
			print("Debug1")
			RightAnimTrack:Play()
		elseif rn == 2 then
			print("Debug2")
			LeftAnimTrack:Play()
		end
		-----------------------
		wait(waitTime)
		debounce = false
	end

end)

It prints it, but doesn’t play the animation.

Try loading the animations outside of the function. Maybe they aren’t getting loaded fast enough. Plus, you don’t need to do it everytime the tool is activated anyways, only at the beginning of the script.

Alright, so I did this:

local leftPunch = script:WaitForChild("LeftPunch")
local rightPunch = script:WaitForChild("RightPunch")
local tool = script.Parent


local debounce = false
local waitTime = 0.4

local Character = tool.Parent
local Humanoid = Character.Humanoid -- Error Line

local RightAnimTrack = Humanoid:LoadAnimation(rightPunch)
local LeftAnimTrack = Humanoid:LoadAnimation(leftPunch)

script.Parent.Activated:Connect(function()
	if debounce == false then
		debounce = true
		-----------------------
		local rn = math.random(1,2)

		if rn == 1 then
			print("Debug1")
			RightAnimTrack:Play()
		elseif rn == 2 then
			print("Debug2")
			LeftAnimTrack:Play()
		end
		-----------------------
		wait(waitTime)
		debounce = false
	end

end)

And I get this error:


It points to line 10.

The tool is inside the character’s backpack, fix your indexing to access the character (one more up)

The script is still inside the backpack, you need to run the code when it gets equipped.

maybe he should do a nested connection then? Equipped with Activated inside (He can use the :Once event for the inner connection to avoid any memory leak)

I would save the animations in a variable (initially set to nil) whenever its equipped so the activated event can play them after.

1 Like
local activated = nil
local animation = nil

tool.Equipped:Connect(function()
    -- Load animations in this block...
    animation = humanoid:WaitForChild("Animator"):LoadAnimation(...)

    activated = tool.Activated:Connect(function()
        -- Play animations...
    end)
end)

tool.Unequipped:Connect(function()
    activated:Disconnect()
end)

@Shabo_Neeno @aiden9486 something like this?
I get this strange feeling there’s an easier way to do this but I just can’t pinpoint it lol. I’m a little rusty

So I put it in my code,

local leftPunch = script:WaitForChild("LeftPunch")
local rightPunch = script:WaitForChild("RightPunch")
local tool = script.Parent


local debounce = false
local waitTime = 0.4

local activated = nil
local animation = nil
local animation2 = nil

tool.Equipped:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid

	--local RightAnimTrack = Humanoid:LoadAnimation(rightPunch)
	--local LeftAnimTrack = Humanoid:LoadAnimation(leftPunch)
	
	animation = Humanoid:WaitForChild("Animator"):LoadAnimation(leftPunch)
	animation2 = Humanoid:WaitForChild("Animator"):LoadAnimation(rightPunch)

	activated = tool.Activated:Connect(function()
		if debounce == false then

			debounce = true
			-----------------------
			local rn = math.random(1,2)

			if rn == 1 then
				print("Debug1")
				animation:Play()
			elseif rn == 2 then
				print("Debug2")
				animation2:Play()
			end
			-----------------------
			wait(waitTime)
			debounce = false
		end

	end)
end)

tool.Unequipped:Connect(function()
	activated:Disconnect()
end)

Same issue, doesn’t work, but this time no errors.

debug is still printed though right?

Yes. Debug prints every time I mouse click, random one. So the random number thing works, it’s something with the animation code.

while you’re still in the game testing, can you send a picture of your player’s backpack? Under the Players service