Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Gingerely 245 Moderation Voter
4 years ago
Edited 4 years ago

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)
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. User#20279 0 — 4y
0
did not work Gingerely 245 — 4y
0
Can you post what you edited in your code? Also did you get any errors or warnings? User#20279 0 — 4y
0
I did not have any errors and here is the code Gingerely 245 — 4y
View all comments (8 more)
0
I edited the post so I added a wait before line 11. Gingerely 245 — 4y
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. User#20279 0 — 4y
0
Weird... IT is a local script. It wont work ;-; Gingerely 245 — 4y
0
OOOOH I SEE! The animation was not good. Gingerely 245 — 4y
0
@Denny9876 so when i use an auto clicker it re-plays the script real fast. Is there a way to fix that? Gingerely 245 — 4y
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. User#20279 0 — 4y
0
Nvm, fixed and yes i used debounce ^^ thanks <3 Gingerely 245 — 4y
0
Answer would be awesome thanks ^^ Gingerely 245 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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!

0
Thank you so much :) Gingerely 245 — 4y
Ad

Answer this question