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

Can someone help me with my animation playing for my tool?

local plname = tostring(game.Players.LocalPlayer.Name)
local pl = game.Workspace:WaitForChild(plname)
local planimation = pl.Humanoid:LoadAnimation(script.Parent.BatonAnimation)

local debounce = 0

script.Parent.Activated:Connect(function()
  if debounce == 0 then
	debounce = 1
	planimation:Play()
	wait(0.5)
	debounce = 0
 end
end)

When you click, it is supposed to play but it dosen't. Plus, it gives no error. Thanks!

4 comments

0

Is it a LocalScript?

0

Yes

0

you don't need to wrap a string in "tostring"

0

Did I answer your question, or are you confused by anything still?

2 answers

✓ Accepted answer

There are no errors in your code, however, I'm guessing you haven't messed with the animation settings, you need to set the animation priority to "Action" as right now the other animations (roblox default ones) are weighted more so they will always play over your animation.

Also don't do the whole

local pl = tostring(game.Players.LocalPlayer.Name)

instead you can just get the humanoid from the players character directly, so just do

local pl = game.Players.LocalPlayer
local Char = pl.Character or pl.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")

please don't just copy this code, try to understand, but here is what it should look like at the end

local pl = game.Players.LocalPlayer
local Char = pl.Character or pl.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local planimation = Humanoid:LoadAnimation(script.Parent.BatonAnimation)
local debounce = 0
	 
script.Parent.Activated:Connect(function()
	if debounce == 0 then
		debounce = 1
		planimation:Play()
		wait(0.5)
		debounce = 0
	end
end)

What you need is to use AnimationTrack.Priority, so you need to set the animation priority for your BatonAnimation to action, so it overrides roblox's default animations. It can be done as shown:

local Player = game.Players.LocalPlayer.Name
local Character = player.Character
local BatonAnimation= pl.Humanoid:LoadAnimation(script.Parent.BatonAnimation)
BatonAnimation.Priority = Enum.AnimationPriority.Action --this

local debounce = true

script.Parent.Activated:Connect(function()
	if debounce then
		debounce = false
		planimation:Play()
		wait(0.5)
		debounce = true
	end
end)

Since this answers your question, then mark it as the accepted answer.

And here are some things you need to understand about the basics of scripting:

So from inside a local script, you can get the player's character directly by getting the localplayer, and then referencing the character property of the player. It makes it much easier and you don't need to mess with any WaitForChild stuff. And since this is in a tool, you also don't need to wait for their character to load because the tools get loaded after the character does.

Also, your naming conventions are a bit unhelpful when explaining exactly what the variable means. Anyone should be able to look at the variable anywhere in your script and be able to get a gist of what it is referring to.

Third, your debounce using an integer is a bit inefficient, it is better to use a boolean value so that you can just check its value by doing if debounce then rather than using if debounce == 0 then, its just shorter and it uses up less resources even if its not much to really matter.

And last, you need to keep Roblox's default indentation, when I copied your code, I noticed you deleted the proper indents and replaced them with your own 2 space indents. Its not a problem in that it would make your code not work, its just that keeping the original indents makes your code easier to read and find errors in.

3 comments

0

Many things are wrong here, but I did use the Enum.AnimationPriority so thanks but the other answer helped more

0

Actually no, mine actually answers your question with the information you need. I even included the extra stuff for your benefit, so I fail to see how his helped you more.

0

"planimation" Isn't even defined AND EVEN A VARIABLE! Same with many other things

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