My goal is to make a damage system that damages people with a DEBOUNCE. I have a folder of Bandits and want to make sure if any of the bandits touches an enemy they get damaged. I want to avoid loops as much as possible.
--// Variables \\-- local RunService = game:GetService("RunService") local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS) local Respawn_Time = Settings.BANDIT.RESPAWN_TIME local plrServ = game:GetService('Players') local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit local function DamageEnemy(NPC, h, Damage) h.Parent.Humanoid:TakeDamage(Damage) end local function TouchFunction(Bandit) local Debounce = true local DamageAmount = Settings.BANDIT.DAMAGE Bandit.Touched:Connect(function(h) if Debounce and h.Parent.Name ~= Bandit.Parent.Name and h.Parent:FindFirstChild('Humanoid') then Debounce = false DamageEnemy(Bandit, h, DamageAmount) wait(2) Debounce = true end end) end for _, Bandit in pairs(workspace.Bandits:GetDescendants()) do if Bandit:IsA('BasePart') then spawn(function() TouchFunction(Bandit) end) end end
Anyway,
There is no real error but for some reason when I die, everything stops working. The PROBLEM is the DEBOUNCE doesn't work.
Basically: Everything works in terms of damaging but it seems like the Debounce has no effect.
Evidence: https://gyazo.com/267466299848731ce7e2209f03d6eb4f ~ In studio https://gyazo.com/7af3ca54c0c7782f3018a2d431993fb3 ~ In actual Server
I appreciate and thank you if you provide any helpful feedback on how to solve this issue or how to improve the code in general.
Thank you for reading.
I've tried this script with just a folder full of parts called Bandits, and it worked:
--// Variables \\-- local RunService = game:GetService("RunService") --local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS) --local Respawn_Time = Settings.BANDIT.RESPAWN_TIME local plrServ = game:GetService('Players') --local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit local function DamageEnemy(NPC, h, Damage) h.Parent.Humanoid:TakeDamage(Damage) end local function TouchFunction(Bandit) local Debounce = true local DamageAmount = 5 Bandit.Touched:Connect(function(h) if Debounce and h.Parent.Name ~= Bandit.Parent.Name and h.Parent:FindFirstChild('Humanoid') then Debounce = false DamageEnemy(Bandit, h, DamageAmount) wait(2) Debounce = true end end) end for _, Bandit in pairs(workspace.Bandits:GetDescendants()) do if Bandit:IsA('BasePart') then TouchFunction(Bandit) end end
So that means that the glitch happened on one of the lines that I've commented out- in other words, something related to the ModuleScript. My guess is something to do with the respawning is resetting the bandits, disconnecting the event that the old bandits were using. To fix this (if you are adding new bandits to the Bandits folder), you could try using ChildAdded:
local function AddBandit(Bandit) if Bandit:IsA('BasePart') then TouchFunction(Bandit) end end for _,Bandit in pairs(workspace.Bandits:GetDescendants()) do AddBandit(Bandit) end workspace.Bandits.ChildAdded:Connect(function(Bandit) AddBandit(Bandit) end)
to keep the script up-to-date with new bandits.
Otherwise, you can try work back from the test that I used to find any other potential errors.
The problem is, you didn't use the debounce to see if the loop should run or not.
for i,v in pairs(workspace.Bandits:GetDescendants()) do if v:IsA("BasePart") then local TouchDebounce = true v.Touched:Connect(function(h) if TouchDebounce and h.Parent.Name ~= v.Parent.Name and h.Parent:FindFirstChild('Humanoid') and not TouchDebounce then --Here, you see if another touch is happening. TouchDebounce = true local Hum = h.Parent.Humanoid local DamageAmount = Settings.BANDIT.DAMAGE dmgfunction(v, Hum, DamageAmount) wait(2) TouchDebounce = false end end) end end
Fix:
--// Variables \\-- local RunService = game:GetService("RunService") local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS) local Respawn_Time = Settings.BANDIT.RESPAWN_TIME local plrServ = game:GetService('Players') local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit local function DamageEnemy(NPC, h, Damage) h.Parent.Humanoid:TakeDamage(Damage) end local function TouchFunction(Bandit) local Debounce = true local DamageAmount = Settings.BANDIT.DAMAGE Bandit.Touched:Connect(function(h) if Debounce == true and h.Parent.Name ~= Bandit.Parent and h.Parent:FindFirstChild('Humanoid') then Debounce = false DamageEnemy(Bandit, h, DamageAmount) wait(2) Debounce = true end end) end for _, Bandit in pairs(workspace.Bandits:GetDescendants()) do if Bandit:IsA('BasePart') then spawn(function() TouchFunction(Bandit) end) end end
You forgot the == true, I think. Or == false.