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

I made a tool that would play with the animation but it did not work can anyone help me ?

It gives me the strength but however it does not play the animation...

local tool = script.Parent
local plr = game.Players.LocalPlayer 
local Character = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://4139476411"
local PlayAnim = Humanoid:LoadAnimation(Anim)
tool.Activated:Connect(function()
	PlayAnim:Play()
	wait(.7)
	plr.leaderstats.Strength.Value =  plr.leaderstats.Strength.Value + 1
	PlayAnim:Stop()
end)

12 comments

0

You’re playing the animation but then immediately stopping it afterwards, so it’ll look like it won’t play at all. You could either remove line 11 or add a wait before it.

0

did not work

0

Can you post what you edited in your code? Also did you get any errors or warnings?

0

I did not have any errors and here is the code

0

I edited the post so I added a wait before line 11.

0

It seems to work for me. Is this a local script? If not, it won't work as LocalPlayer is only available through local scripts. Also check to make sure your animation is designed properly.

0

Weird... IT is a local script. It wont work ;-;

0

OOOOH I SEE! The animation was not good.

0

@Denny9876 so when i use an auto clicker it re-plays the script real fast. Is there a way to fix that?

0

^ You use a debounce for that. I'll make an answer to explain it. Also I found another problem which I will also explain.

0

Nvm, fixed and yes i used debounce ^^ thanks <3

0

Answer would be awesome thanks ^^

1 answer

✓ Accepted answer

Okay, so I had to rewrite your code as I noticed a big problem with your script. You're using LocalPlayer which only works in local scripts, but changing the leaderstats on a local script will only show for you and not everyone else.

I fixed this by making the script a server (normal) script and getting the player that equipped the tool instead of the LocalPlayer. I also added a debounce to prevent spam.

Here's the code:

local Players = game:GetService("Players")

local tool = script.Parent
local plr
local Character
local Humanoid
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://4139476411"
local PlayAnim

local debounce = false

tool.Activated:Connect(function()
	if debounce == true then
		return -- Function will stop here if debounce is true to prevent spam.
	end
	debounce = true
	if PlayAnim ~= nil then -- Checks if animation loaded to prevent errors
		PlayAnim:Play()
		wait(.7)
		plr.leaderstats.Strength.Value =  plr.leaderstats.Strength.Value + 1
		PlayAnim:Stop()
	end
	debounce = false -- After the animation plays, debounce will be false so tool can be used again.
end)

tool.Equipped:Connect(function()
	plr = Players:GetPlayerFromCharacter(script.Parent.Parent) -- Gets the player that equipped the item
	if plr ~= nil then
		Character = plr.Character
		Humanoid = Character:WaitForChild("Humanoid")
		PlayAnim = Humanoid:LoadAnimation(Anim)
	end
end)

Hope this helped! Sorry for making all this complicated. If you have any questions, just comment!

1 comment

0

Thank you so much :)

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