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.
01 | --// Variables \\-- |
02 | local RunService = game:GetService( "RunService" ) |
03 | local Settings = require(game:GetService( "ReplicatedStorage" ).ModuleScripts.SETTINGS) |
04 | local Respawn_Time = Settings.BANDIT.RESPAWN_TIME |
05 | local plrServ = game:GetService( 'Players' ) |
06 | local NPCBackup = game:GetService( 'ServerStorage' ).NPCBackup.Bandit |
07 |
08 | local function DamageEnemy(NPC, h, Damage) |
09 | h.Parent.Humanoid:TakeDamage(Damage) |
10 | end |
11 |
12 | local function TouchFunction(Bandit) |
13 | local Debounce = true |
14 | local DamageAmount = Settings.BANDIT.DAMAGE |
15 | Bandit.Touched:Connect( function (h) |
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:
01 | --// Variables \\-- |
02 | local RunService = game:GetService( "RunService" ) |
03 | --local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS) |
04 | --local Respawn_Time = Settings.BANDIT.RESPAWN_TIME |
05 | local plrServ = game:GetService( 'Players' ) |
06 | --local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit |
07 |
08 | local function DamageEnemy(NPC, h, Damage) |
09 | h.Parent.Humanoid:TakeDamage(Damage) |
10 | end |
11 |
12 | local function TouchFunction(Bandit) |
13 | local Debounce = true |
14 | local DamageAmount = 5 |
15 | Bandit.Touched:Connect( function (h) |
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:
01 | local function AddBandit(Bandit) |
02 | if Bandit:IsA( 'BasePart' ) then |
03 | TouchFunction(Bandit) |
04 | end |
05 | end |
06 | for _,Bandit in pairs (workspace.Bandits:GetDescendants()) do |
07 | AddBandit(Bandit) |
08 | end |
09 | workspace.Bandits.ChildAdded:Connect( function (Bandit) |
10 | AddBandit(Bandit) |
11 | 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.
01 | for i,v in pairs (workspace.Bandits:GetDescendants()) do |
02 | if v:IsA( "BasePart" ) then |
03 | local TouchDebounce = true |
04 | v.Touched:Connect( function (h) |
05 | 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. |
06 | TouchDebounce = true |
07 | local Hum = h.Parent.Humanoid |
08 | local DamageAmount = Settings.BANDIT.DAMAGE dmgfunction(v, Hum, DamageAmount) |
09 | wait( 2 ) |
10 | TouchDebounce = false |
11 | end |
12 | end ) |
13 | end |
14 | end |
Fix:
01 | --// Variables \\-- |
02 | local RunService = game:GetService( "RunService" ) |
03 | local Settings = require(game:GetService( "ReplicatedStorage" ).ModuleScripts.SETTINGS) |
04 | local Respawn_Time = Settings.BANDIT.RESPAWN_TIME |
05 | local plrServ = game:GetService( 'Players' ) |
06 | local NPCBackup = game:GetService( 'ServerStorage' ).NPCBackup.Bandit |
07 |
08 | local function DamageEnemy(NPC, h, Damage) |
09 | h.Parent.Humanoid:TakeDamage(Damage) |
10 | end |
11 |
12 | local function TouchFunction(Bandit) |
13 | local Debounce = true |
14 | local DamageAmount = Settings.BANDIT.DAMAGE |
15 | Bandit.Touched:Connect( function (h) |
You forgot the == true, I think. Or == false.