Dodgy animation loading

Hello! I’m currently trying to get a reaction animation when an enemy humanoid is punched, for the most part, it works, but for some reason, it occasionally spits out ’ LoadAnimation requires an Animation object’, even though there should be a humanoid there. It is really puzzling me because it works sometimes but then doesn’t the next. All of the animations are preloaded and work, its just this part of the script. It is also using R6. (This is my first post on the devforum so please give me feedback) Thanks!

local anims = {
	animations:WaitForChild("punch1"), animations:WaitForChild("punch2"), animations:WaitForChild("punch3"), animations:WaitForChild("punch4"),
}
local eanims = {
	enemyanims:WaitForChild("enemypunch1")
}
local limbs = {
	"Right Arm", "Left Arm", "Right Arm", "Right Arm"
}

	hitbox.Touched:Connect(function(hit)
		if hit:IsA("BasePart") then
			if not hit:IsDescendantOf(char) then
				local enemyhum = hit.Parent:FindFirstChild("Humanoid")
				local enemyroot = hit.Parent:FindFirstChild("HumanoidRootPart")
				if enemyhum and enemyroot then
					hitbox:Destroy()
					
					enemyhum:TakeDamage(damage)
					local react = enemyhum:LoadAnimation(eanims[count])

					react:Play()
			
					end
				end
			end
		end
	end)

If you need the whole script i can add it, i just thought it was quite large and chunky so i snipped it a bit. (this is also due to lack of experience on the developer forums)

1 Like

is count returning 1 or 2 when this problem happens?
because if it returns 2 then it will return nil when you try to index eanims[count]

1 Like

Its returning 2. Also thankyou for quick response.

1 Like

ok, for now a quick solution would just be to change count to 1 unless you plan on adding more animations to the table, because anything after 1 will just be nil, it will work then.

1 Like