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

How do you add a debounce to two different functions?

I'm trying to make a hammer that has a charging system. I don't want players to just spam click because that will beat the purpose of the charging system? How do I go about it?

This is what I've tried and it's horrible. Please help, thanks!

local tool = script.Parent

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")

local charge = script.Charge
local smash = script.Smash

local chargeTrack = humanoid:LoadAnimation(charge)
local smashTrack = humanoid:LoadAnimation(smash)

local charged = false
local cooldown = false

if not cooldown then
	cooldown = true
	
	tool.Activated:Connect(function()
		if not charged then
			smashTrack:Stop()
			chargeTrack:Play()

			delay(chargeTrack.Length - 0.02999, function()
				chargeTrack:AdjustSpeed(0)
			end)

			charged = true
		end
	end)

	tool.Deactivated:Connect(function()	
		if charged then
			chargeTrack:Stop()
			smashTrack:Play()

			delay(smashTrack.Length - 0.02999, function()
				smashTrack:AdjustSpeed(0)

				rootPart.Anchored = true

				wait(2)
				smashTrack:Stop()

				rootPart.Anchored = false
			end)

			charged = false
		end
	end)	
	
	wait(2)
	cooldown = false
end

1 answer

local tool = script.Parent

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")

local charge = script.Charge
local smash = script.Smash

local chargeTrack = humanoid:LoadAnimation(charge)
local smashTrack = humanoid:LoadAnimation(smash)

local charged = false
local cooldown = false

if not cooldown then
    cooldown = true

    tool.Activated:Connect(function()
        if not charged then
        charged = true -- you put it at the end
            smashTrack:Stop()
            chargeTrack:Play()

            delay(chargeTrack.Length - 0.02999, function()
                chargeTrack:AdjustSpeed(0)
            end)
 -- if charged was there it would set to true after the delay 
        end
    end)

    tool.Deactivated:Connect(function() 
        if charged then
            chargeTrack:Stop()
            smashTrack:Play()

            delay(smashTrack.Length - 0.02999, function()
                smashTrack:AdjustSpeed(0)

                rootPart.Anchored = true

                wait(2)
                smashTrack:Stop()

                rootPart.Anchored = false
            end)

            charged = false
        end
    end)    

    wait(2)
    cooldown = false
end

2 comments

0

Mind elaborating? Tried and it didn't really make things any better.

0

This script is confusing me bc I don't know what action is happening

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