For my game I am working on a pickaxe. Tool.activated: connect (function ()) etc is what I am using to achieve this goal. I put in a denounce (correctly) and the script works. When clicking on assets that are supposed to be clicked there is indeed a debounce. However, when spam clicking on nothing (air) all of a sudden the debounce stops working. What could cause this?
Note, when moving my mouse from air to assets while spamming my left mouse button the debounce stops working too.
Script:
wait(1)
local pickaxe = script.Parent
local plr = game.Players.LocalPlayer
local Charfolder = game.Workspace:WaitForChild("PlayerCharacters")
local mouse = plr:GetMouse()
local Animation = script.Parent:FindFirstChild("SwingAnimation")
local swingAnimation = Charfolder[plr.Name].Humanoid:LoadAnimation(Animation)
local hitEvent = game.ReplicatedStorage.Harvesting.Events:WaitForChild("RewardEvent")
local healthGui = game.ReplicatedStorage.Harvesting["User Interface"]:FindFirstChild("Healthbar")
local effect = game.ReplicatedStorage.Harvesting.Effects:WaitForChild("ParticleEmitter")
local dmg = game.ReplicatedStorage.Harvesting.DamageInfo:FindFirstChild(pickaxe.Name)
local db = false
pickaxe.Activated:connect(function(plr)
if db == false then
db = true
if mouse.Target then
if mouse.Target.Name == "Hitbox" then
local hitbox = mouse.Target
if hitbox:FindFirstChild("Healthbar") == nil then
local clone = healthGui:Clone()
clone.Parent = hitbox
end
local assetInfo = hitbox.Parent:FindFirstChild("Info")
local Gui = hitbox:FindFirstChild("Healthbar")
local division = (dmg.Value / assetInfo.Health.Value)
local indicator = Gui.Background.HealthIndicator
if assetInfo.CurrentHealth.Value <= 100 then
local x = assetInfo.CurrentHealth.Value / 100
indicator.Size = UDim2.new(x-division, 0, 1, 0)
elseif assetInfo.CurrentHealth.Value <= 1000 then
local x = assetInfo.CurrentHealth.Value / 1000
indicator.Size = UDim2.new(x-division, 0, 1, 0)
elseif assetInfo.CurrentHealth.Value <= 10000 then
local x = assetInfo.CurrentHealth.Value / 10000
indicator.Size = UDim2.new(x-division, 0, 1, 0)
elseif assetInfo.CurrentHealth.Value <= 100000 then
local x = assetInfo.CurrentHealth.Value / 100000
indicator.Size = UDim2.new(x-division, 0, 1, 0)
end
assetInfo.CurrentHealth.Value = assetInfo.CurrentHealth.Value - dmg.Value
if assetInfo.CurrentHealth.Value <= 0 then
local eClone = effect:Clone()
eClone.Parent = hitbox.Parent.EffectPart
eClone:Clone()
eClone:Clone()
eClone:Clone()
wait(1)
hitbox.Parent:Destroy()
end
end
end
end
swingAnimation:Play()
wait(1)
db = false
end)
Well, it’s likely that you set the debounce to only be set if the player clicked something. I don’t think you should set a debounce if the player did basically nothing and it probably took 1 if check worth of performance but you do you.
When a debounce is added, it doesn’t completely stop the running of your script. For example, if I was creating a team changer and I added a debounce of 10 seconds using wait(10), the player would be able to press the button, but it would only team them 10 seconds later. If you would like to make your pickaxe completely disabled, you can add a clause like this:
enabled = True
function debounce()
wait(10)
enabled = true
return
end
local coro = coroutine.create(debounce)
if enabled then
if tool.Activated then -- this line won't actually work, change this to whatever activates your tool
enabled = False
coro.resume()
end
end
The tool will now only work every 10 seconds, if you wish for something to happen when its disabled, you can add an else clause.
If this post helped, please mark it as a solution and leave a like, have a good day!
Thanks for the reply, unfortunately the issue has not been fixed by doing this
Current Script:
--// Variables
wait(1)
local pickaxe = script.Parent
local plr = game.Players.LocalPlayer
local Charfolder = game.Workspace:WaitForChild("PlayerCharacters")
local mouse = plr:GetMouse()
local Animation = script.Parent:FindFirstChild("SwingAnimation")
local swingAnimation = Charfolder[plr.Name].Humanoid:LoadAnimation(Animation)
local rewardEvent = game.ReplicatedStorage.Harvesting.Events:WaitForChild("RewardEvent")
local healthGui = game.ReplicatedStorage.Harvesting["User Interface"]:FindFirstChild("Healthbar")
local effect = game.ReplicatedStorage.Harvesting.Effects:WaitForChild("ParticleEmitter")
local CheckDamageEvent = game.ReplicatedStorage.Harvesting.Events.CheckDamageEvent
CheckDamageEvent:FireServer(plr)
wait(.3)
local dmg = pickaxe.Info:FindFirstChild("Damage")
--// Debounce
local enabled = true
local function debounce()
wait(1)
enabled = true
return
end
local coro = coroutine.create(debounce)
--// Script
if enabled then
pickaxe.Activated:connect(function(plr)
if mouse.Target then
if mouse.Target.Name == "Hitbox" then
local hitbox = mouse.Target
if hitbox:FindFirstChild("Healthbar") == nil then
local clone = healthGui:Clone()
clone.Parent = hitbox
end
local assetInfo = hitbox.Parent:FindFirstChild("Info")
local Gui = hitbox:FindFirstChild("Healthbar")
local division = (dmg.Value / assetInfo.Health.Value)
local indicator = Gui.Background.HealthIndicator
if assetInfo.CurrentHealth.Value <= 100 then
local x = assetInfo.CurrentHealth.Value / 100
indicator.Size = UDim2.new(x-division, 0, 1, 0)
elseif assetInfo.CurrentHealth.Value <= 1000 then
local x = assetInfo.CurrentHealth.Value / 1000
indicator.Size = UDim2.new(x-division, 0, 1, 0)
elseif assetInfo.CurrentHealth.Value <= 10000 then
local x = assetInfo.CurrentHealth.Value / 10000
indicator.Size = UDim2.new(x-division, 0, 1, 0)
elseif assetInfo.CurrentHealth.Value <= 100000 then
local x = assetInfo.CurrentHealth.Value / 100000
indicator.Size = UDim2.new(x-division, 0, 1, 0)
end
assetInfo.CurrentHealth.Value = assetInfo.CurrentHealth.Value - dmg.Value
if assetInfo.CurrentHealth.Value <= 0 then
local eClone = effect:Clone()
eClone.Parent = hitbox.Parent.EffectPart
eClone:Clone()
eClone:Clone()
eClone:Clone()
wait(1)
hitbox.Parent:Destroy()
end
end
end
swingAnimation:Play()
end)
enabled = false
coro.resume()
end
I dont really understand what you mean. I am spam clicking so not once a second. Besides, I am getting an error from the line coro.resume()
Error message: ‘attempt to index thread with ‘resume’’
Changing the wait(1) in the part of to wait(10) did not work
local function debounce()
wait(1)
enabled = true
return
end
Update:
I fixed the issue by putting a boolvalue inside game.players.localplayer.folder
This is changed by a remoteevent. Bit strange system perhaps but it works really well. Thank you to all those who tried to help