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

Adding a debounce to a tool.Activated function?

Can someone help me add a debounce to this script? I'm not really sure how to do it, but it still creates multiple parts each time you click, I tried doing it but it still creates multiple parts if you spam click, can someone help?

Discord: Unknown Intelligence#6569

tool = script.Parent
local players = game:GetService("Players")


local function StopAllAnimations()
        local animations = {
        anim1 = players.LocalPlayer.Character.Humanoid:LoadAnimation(tool:WaitForChild("Animation"))
        }
 for i,v in pairs(animations) do
    print("hi")
  v:Stop()
 end
end

tool.Equipped:connect(function()
    print("Equipped")
    bool = false
if not bool then
tool.Activated:Connect(function()
    bool = true
            print("Clicked")
    players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
    local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
        animation:Play()
    local spirit = Instance.new("Part")
    spirit.Anchored = true
    spirit.Name = "spirit"
    spirit.Material = "Neon"
    spirit.Parent = workspace
    spirit.Size = Vector3.new(5,5,5)
    spirit.Shape = "Ball"
    spirit.BrickColor = BrickColor.new("Cyan")
    spirit.CanCollide = false
    spirit.TopSurface = "Smooth"
    spirit.BottomSurface = "Smooth"
    spirit.LeftSurface = "Smooth"
    spirit.RightSurface = "Smooth"
    spirit.FrontSurface = "Smooth"
    spirit.BackSurface = "Smooth"
    spirit.Position = Vector3.new(players.LocalPlayer.Character.Head.Position.X, players.LocalPlayer.Character.Head.Position.Y + 30 ,players.LocalPlayer.Character.Head.Position.Z)
    wait()
    for i = 1,100,1 do
        wait(.1)
    spirit.Size = spirit.Size + Vector3.new(.2,.2,.2) 
        end
        bool = false
    end)
        end
end)

tool.Unequipped:Connect(function()
    StopAllAnimations()
    local spirit = game.workspace:WaitForChild("spirit")
    spirit:Destroy()
    local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation2)
        animation:Play()
    players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
    
end)

5 comments

0

JUst making sure, so it adds a new tool everytime you click, correct?

0

It doesn't add a new tool it's just A tool, when you have the tool equipped there is no handle or anything it's just a tool, what happens is when it's equipped and activated a part spawns above your head and increases in size, but if you spam click the part spams, I need a debaunce for that idk how to do it I tried :\

0

ooh, so there is supposed to be a part, but not increasing in size? sorry if i'm not understanding

0

No it does everything perfectly it's just if you spam click it makes MULTIPLE parts I only want it to make 1 until that part is destroyed aka a debounce but idk how to add that

0

Oh, okay.

1 answer

✓ Accepted answer

Hello, adamson239!

I've made some comments at my changes, so you can see better what I've made

tool = script.Parent
local players = game:GetService("Players")


local function StopAllAnimations()
        local animations = {
        anim1 = players.LocalPlayer.Character.Humanoid:LoadAnimation(tool:WaitForChild("Animation"))
        }
 for i,v in pairs(animations) do
    print("hi")
  v:Stop()
 end
end

local debounce = true

tool.Equipped:connect(function()
    print("Equipped")
-- Removed some lines due to being useless
tool.Activated:Connect(function()
    if debounce == false then return end -- Here we return if debounce is false
    debounce = false -- Here we add the debounce
            print("Clicked")
    players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
    local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
        animation:Play()
    local spirit = Instance.new("Part")
    spirit.Anchored = true
    spirit.Name = "spirit"
    spirit.Material = "Neon"
    spirit.Parent = workspace
    spirit.Size = Vector3.new(5,5,5)
    spirit.Shape = "Ball"
    spirit.BrickColor = BrickColor.new("Cyan")
    spirit.CanCollide = false
    spirit.TopSurface = "Smooth"
    spirit.BottomSurface = "Smooth"
    spirit.LeftSurface = "Smooth"
    spirit.RightSurface = "Smooth"
    spirit.FrontSurface = "Smooth"
    spirit.BackSurface = "Smooth"
    spirit.Position = Vector3.new(players.LocalPlayer.Character.Head.Position.X, players.LocalPlayer.Character.Head.Position.Y + 30 ,players.LocalPlayer.Character.Head.Position.Z)
    wait()
    for i = 1,100,1 do
        wait(.1)
    spirit.Size = spirit.Size + Vector3.new(.2,.2,.2) 
        end
    wait(1) -- Debounce time in seconds
       debounce = true
    end)
end)

tool.Unequipped:Connect(function()
    StopAllAnimations()
    local spirit = game.workspace:WaitForChild("spirit")
    spirit:Destroy()
    local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation2)
        animation:Play()
    players.LocalPlayer.Character.Humanoid.WalkSpeed = 16

end)

Sorry for the identation.

Warning: This code was not tested. IF you encounter any errors, you can contact me.

2 comments

0

Thank you so much! Mind telling me if I was on the right track to debouncing? I really need to learn more about scripting and that's something I've always had a problem with :\

1

Cail now its your turn to answer my post. XD. (this was a joke, but still, please. lol)

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