hello i need help with debounce (deb) all i want to do is “cooldown” the function, as of the moment whenever i get ingame i can spam the tool and it creates lag, my objective is to whenever i click once the tool goes on cd for 5 seconds and after the said time i can click it again!
Also looking for ways to improve the code as i can feel it being a bit messy
{CODE}
local deb = false
local attachmode = Instance.new(“Attachment”)
local attachthunder = Instance.new(“Attachment”)
local glow = script.HaoriTorso.Torso.Mode.Glow:Clone()
local star = script.HaoriTorso.Torso.Mode.Star:Clone()
local shocks = script.HaoriTorso.Torso.Mode.Shocks:Clone()
local bolt = script.HaoriTorso.Torso.Thunderbolt.ParticleEmitter:Clone()
local lightbolt = script.HaoriTorso.Torso.Mode.PointLight:Clone()
script.Parent.Activated:Connect(function()
local Character = script.Parent.Parent
local oldws = Character.Humanoid.WalkSpeed
local modeanim = script.HaoriTorso.Torso.Attachment.ModeAnim
local hum = Character.Humanoid
local track = hum:LoadAnimation(modeanim)
wait(0.05)
track:Play()
hum.WalkSpeed = 50
attachmode.Parent = Character.Torso
attachthunder.Parent = Character.Torso
attachthunder.CFrame = CFrame.new(0, 35, 0)
glow.Parent = attachmode
star.Parent = attachmode
shocks.Parent = attachmode
lightbolt.Parent = attachmode
bolt.Parent = attachthunder
glow:Emit(1)
star:Emit(1)
shocks:Emit(1)
bolt:Emit(1)
lightbolt.Enabled = true
wait(0.05)
lightbolt.Enabled = false
local Game = game
local Players = Game:GetService("Players")
if not Character:FindFirstChild("HaoriTorso") and deb == false then
deb = true
local Haori = script.HaoriTorso:Clone()
Haori:SetPrimaryPartCFrame(Character.Torso.CFrame)
local weld = Instance.new("ManualWeld")
weld.Part0 = Haori.PrimaryPart
weld.Part1 = Character.Torso
weld.Parent = weld.Part0
for i, v in pairs (Haori:GetChildren()) do
local weld2 = Instance.new("WeldConstraint")
weld2.Part0 = Character.Torso
weld2.Part1 = v
weld2.Parent = weld.Part0
v.Anchored = false
v.Massless = true
end
Haori.Parent = Character
wait(5)
deb = false
elseif Character:FindFirstChild("HaoriTorso") and deb == false then
deb = true
Character:FindFirstChild("HaoriTorso"):Destroy()
hum.WalkSpeed = 16
end
deb = false
Please format the entire script as it’s difficult to read in 2 sections.
You are activating the tool, then setting a bunch of the variables, playing the track, setting attachments and parameters, enabling the lightbolt, waiting .05 seconds (should be task.wait(.05) by the way), going to the Players service and THEN checking to see if deb == false.
The deb check should be done right after the function is activated so that none of that other stuff is done.
Here is a corrected and improved version of the code with comments in addition to task.wait being used (you can read @Scottifly’s respone to know why above ^^^)
IMPROVEMENTS:
Cooldown: Added a waiting time of 5 seconds after using the tool before it can be used again.
Debounce: Prevented the tool from being activated too quickly by adding a short delay between uses.
Function Encapsulation: Organized the tool’s actions into a clear set of steps, making it easier to manage and modify.
task.wait: Used a faster and more efficient method for waiting in the game.
local debounce = false
local cooldown = 5 -- Cooldown time in seconds
local attachmode = Instance.new("Attachment")
local attachthunder = Instance.new("Attachment")
local glow = script.HaoriTorso.Torso.Mode.Glow:Clone()
local star = script.HaoriTorso.Torso.Mode.Star:Clone()
local shocks = script.HaoriTorso.Torso.Mode.Shocks:Clone()
local bolt = script.HaoriTorso.Torso.Thunderbolt.ParticleEmitter:Clone()
local lightbolt = script.HaoriTorso.Torso.Mode.PointLight:Clone()
local function ActivateTool()
if not debounce then -- If the tool is not on cooldown (not being used at the moment)
debounce = true -- Set the debounce to true to prevent multiple activations at once
local Character = script.Parent.Parent
local oldws = Character.Humanoid.WalkSpeed
local modeanim = script.HaoriTorso.Torso.Attachment.ModeAnim
local hum = Character.Humanoid
local track = hum:LoadAnimation(modeanim)
wait(0.05)
track:Play()
hum.WalkSpeed = 50 -- Increase the player's walk speed while using the tool
attachmode.Parent = Character.Torso -- Attach the magic effects to the player's torso
attachthunder.Parent = Character.Torso
attachthunder.CFrame = CFrame.new(0, 35, 0) -- Move the thunder effect to a specific position
glow.Parent = attachmode
star.Parent = attachmode
shocks.Parent = attachmode
lightbolt.Parent = attachmode
bolt.Parent = attachthunder -- Parent the magic effects to their corresponding attachments
glow:Emit(1)
star:Emit(1)
shocks:Emit(1)
bolt:Emit(1) -- Emit (activate) the magic effects
lightbolt.Enabled = true -- Enable the point light effect
wait(0.05)
lightbolt.Enabled = false -- Disable the point light effect after a short delay
if not Character:FindFirstChild("HaoriTorso") then
-- If the player doesn't have the magic effect already
local Haori = script.HaoriTorso:Clone()
Haori:SetPrimaryPartCFrame(Character.Torso.CFrame)
local weld = Instance.new("ManualWeld")
weld.Part0 = Haori.PrimaryPart
weld.Part1 = Character.Torso
weld.Parent = weld.Part0
for i, v in pairs(Haori:GetChildren()) do
local weld2 = Instance.new("WeldConstraint")
weld2.Part0 = Character.Torso
weld2.Part1 = v
weld2.Parent = weld.Part0
v.Anchored = false
v.Massless = true
end
Haori.Parent = Character -- Attach the magic effect to the player
else
-- If the player already has the magic effect
Character:FindFirstChild("HaoriTorso"):Destroy() -- Remove the magic effect
hum.WalkSpeed = 16 -- Set the player's walk speed back to normal
end
wait(cooldown) -- Wait for the cooldown duration (5 seconds)
debounce = false -- Reset the debounce to allow the tool to be used again
end
end
script.Parent.Activated:Connect(ActivateTool) -- When the tool is clicked, activate the magic power
thank you for posting this
i was trying really hard to do it “my” way but i just blocked and when i had fixed the debounce another issue popped and left me in shambles
your writting fixed everything the script had flawed!