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

Animation Won't Play When The Mouse Clicks?

So I am creating a simulator and most people know that when you are holding the tool or weapon and you click it plays and animation and gives strength. I am not focusing on the strength yet but I just need help on the animation. So the tool I am using is a wand and when you are holding it and you click, the animation will play. Everything is fine except for the animation. The animation won't play. The AnimationId is correct and and the animation is not nil. Everything but the "AnimationIsNil" is printing. I have moved some lines to other places and it still does not work. If any of you have a solution please tell me.

This is a local script in the StarterPlayerScripts

local mouse = game.Players.LocalPlayer:GetMouse()
local Character = game.Players.LocalPlayer.Character
if not Character or not Character.Parent then
	Character = game.Players.LocalPlayer.CharacterAdded:Wait()
end
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local WandMovement = Instance.new("Animation")
WandMovement.AnimationId = "rbxassetid://6602046971"
local Tool = game.Players.LocalPlayer.Backpack:FindFirstChildWhichIsA("Tool")

Tool.Equipped:Connect(function()
	print("ToolEquippedFUnction")
	mouse.Button1Down:Connect(function()
		print("IntoMouseButtonFunction")
		local WandMovementAnimationTrack = Animator:LoadAnimation(WandMovement)
		WandMovementAnimationTrack:Play()
		if WandMovementAnimationTrack == nil then
			print("animationtrackisnil")
		else
			print("AnimationIsNotNil")
		end
	end)
end)

Thank you for reading!

1 comment

0

Also the priority of the Animation is Action

2 answers

✓ Accepted answer

It will only play the animation when you equip and click at the same frame since it only fires the mouse click function when Tool equipped event is fired. So do this instead:

debounce = false
local Character = game.Players.LocalPlayer.Character
if not Character or not Character.Parent then
	Character = game.Players.LocalPlayer.CharacterAdded:Wait()
end
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local WandMovement = Instance.new("Animation")
WandMovement.AnimationId = "rbxassetid://6602046971"
local Tool = game.Players.LocalPlayer.Backpack:FindFirstChildWhichIsA("Tool")

Mouse.Button1Down:Connect(function()
	if debounce = false then
		debounce = true
		if Character:FindFirstChild(""..Tool.Name) then
			print("IntoMouseButtonFunction")
			local WandMovementAnimTrack = Animator:LoadAnimation(WandMovement)
			WandMovementAnimTrack:Play()
			if WandMovementAnimTrack == nil then
				print("animationtrackisnil")
			else
				print("AnimationIsNotNil")
			end
		end
	else
		return
	end
end)

Mouse.Button1Up:Connect(function()
	wait()
	debounce = false
end)

I added debounce (cooldown) in case that was the problem.

5 comments

0

Sorry, but this script sadly did not work. Thank you for trying to help me though! :)

0

oof ok

0

OH MY GOD IM AN IDIOT I mispelled debounce to denounce lmao

0

there i fixed the typo

0

Ok I just figured out I needed to add one more thing and it worked! Thank you!

Instead of

Animator:LoadAnimation(WandMovement)

you should try doing

Humanoid:LoadAnimation(WandMovement)

If this doesn't work, try changing the animation priority. I hope this helped!

2 comments

0

I have tested it but it still has not work. Thank you leaving an answer to help me! :)

0

Humanoid:LoadAnimation is deprecated. Animator:LoadAnimation is correct

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