Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

No matter what I do, I am unable to have my animation play along with my attack. How do I fix this?

Hey, I'm new. Most of my code is from other people, and I'm not very knowledgeable. I have tried many things so far, and this is the current state of my code:

local tool = script.Parent

local canDamage = false

local Animation2 = Instance.new("Animation4")
Animation.AnimationId = "rbxassetid://9119859474"

local function onTouch(otherPart)



	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")



	if not humanoid then 

		return 

	end



	if humanoid.Parent ~= tool.Parent and canDamage then 

		humanoid:TakeDamage(5)

	else

		return

	end



	canDamage = false

end



local function slash()

	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	local AnimationTrack = Humanoid:LoadAnimation(Animation2)
	AnimationTrack:Play()
	CanDamage = true
end



tool.Activated:Connect(slash)

tool.Handle.Touched:Connect(onTouch)

The script is in starterpack, and is in a tool named bonky with the body and the animation, a keyframe sequence, which I'm not entirely sure is a correct format but could not get it into any other format. I have been frustrated for a long time, would appreciate comprehensive answers.

1 answer

How's your day been?

I have a solid solution for you.

I have tried testing the code, yet for some reason it doesn't play the animation.

I am not too experienced with animation, so there's probably an explanation, but for now I can't get the animation to work.

Also, 'Animation4' is not an instance. Try 'Animation' instead.

I have added changes to your script, let me show you:

local tool = script.Parent

local canDamage = false

local Animation = Instance.new("Animation") -- Animation4 was not an instance, I have replaced it with Animation.
Animation.AnimationId = "rbxassetid://9119859474"

local function onTouch(otherPart)



	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")



	if not humanoid then 

		return 

	end



	if humanoid.Parent ~= tool.Parent and canDamage == false then 

		humanoid:TakeDamage(5) -- If this is in a LocalScript, this wouldn't be smart, as the player your fighting would die on your screen, but they are actually still alive.

	else

		return

	end



	canDamage = false

end



local function slash()

	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	local AnimationTrack = Humanoid:LoadAnimation(Animation) -- Replaced Animation2 with Animation. Also, Humanoid:LoadAnimation() is deprecated, try Humanoid.Animator:LoadAnimation().
	AnimationTrack:Play()
	local CanDamage = true -- I was also getting a warning here, saying CanDamage is only used in the enclosing function. I added local at the front so it wouldn't bother me.
end



tool.Activated:Connect(slash)

tool.Handle.Touched:Connect(onTouch)

Just know that I was not able to test this animation, since Roblox wasn't allowing me to switch my game to R6.

Tell me if you have any problems.

This discussion is preserved as it was. New answers, comments, and votes are closed.