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

How would I limit tool to one use without deleting the tool?

Asked by 3 years ago

I am currently working on a game and when you use a tool it will play an animation and pick randomly from a table but the issue is that you can use the tool multiple times and it would pick more then one item from the table and play the animation multiple times. The animation also uses the tool as a prop so it can't be deleted. How would I approach making it so you still have the tool but the function only runs once?

tool = script.Parent
handle = tool:WaitForChild("Handle")
plr = game.Players.LocalPlayer

tool.Activated:Connect(function()

    local Stands = {
        "StarPlatinum", 
        "TheWorld"
    } --The names of the stands

    local anim = Instance.new("Animation", plr.Character)
    anim.AnimationId = "http://www.roblox.com/asset/?id=6813851472"
    local pAnim = plr.Character:WaitForChild("Humanoid"):LoadAnimation(anim)


    local value = math.random(1,2) --the second number is the amount of stands in the table. if you add a new stand change this number.
    local pickedValue = Stands[value]
    print(tostring(pickedValue))

    pAnim:Play()

    wait(3)

    tool:Destroy()

    pAnim:Stop()

end)

0
 You Are the 123456th Person Who Questioned! Brioche_Noodle 45 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

You could add this line at the start of the script:

local itemUsed = false

And in the function you put everything in this "if" statement:

if not itemUsed then
    -- Everything from line 7 to line 27
end

And at line 28 you add this:

itemUsed = true

Hope this helps!

0
Yes it did! Thanks so much! raffkaisa 12 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

You can set Tool.Enabled to false if you do not want the Activated event to be fired again.

0
Oh it's literally that easy lol thanks! raffkaisa 12 — 3y
0
Just tested this, doesn't work. It sets the tool to not enabled but still lets the function run raffkaisa 12 — 3y

Answer this question