Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Debounces for NPC damaging does nothing? How come? Why? [?]

Asked by 8 years ago
Edited 6 years ago

Goal:

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.

Script (This is a part):

01--// Variables \\--
02local RunService = game:GetService("RunService")
03local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS)
04local Respawn_Time = Settings.BANDIT.RESPAWN_TIME
05local plrServ = game:GetService('Players')
06local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit
07 
08local function DamageEnemy(NPC, h, Damage)
09    h.Parent.Humanoid:TakeDamage(Damage)
10end
11 
12local function TouchFunction(Bandit)
13    local Debounce = true
14    local DamageAmount = Settings.BANDIT.DAMAGE
15    Bandit.Touched:Connect(function(h)
View all 29 lines...

Anyway,

Error:

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.

0
Could you give a bit more information about what you're trying to do and what kind of script you're using? TrippyV 314 — 8y
0
I'm trying to make a claim button when you hatch a egg and click claim the element will go into players inventory BlackOrange3343 2676 — 8y
0
What line in the code you posted I sent the error on? Creeperman1524 120 — 6y
0
Line 34, so basically the if statement BlackOrange3343 2676 — 6y
View all comments (4 more)
0
what the hell kind of script is this?! NsNidPL 41 — 6y
0
Damage script BlackOrange3343 2676 — 6y
0
Server script BlackOrange3343 2676 — 6y
0
Put the debounce outside of function. On the code that is triggers, put `if debounce then return end`. Then, immediately change the debounce boolean type. Try this. Zafirua 1348 — 6y

3 answers

Log in to vote
1
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

I've tried this script with just a folder full of parts called Bandits, and it worked:

01--// Variables \\--
02local RunService = game:GetService("RunService")
03--local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS)
04--local Respawn_Time = Settings.BANDIT.RESPAWN_TIME
05local plrServ = game:GetService('Players')
06--local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit
07 
08local function DamageEnemy(NPC, h, Damage)
09    h.Parent.Humanoid:TakeDamage(Damage)
10end
11 
12local function TouchFunction(Bandit)
13    local Debounce = true
14    local DamageAmount = 5
15    Bandit.Touched:Connect(function(h)
View all 29 lines...

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:

01local function AddBandit(Bandit)
02    if Bandit:IsA('BasePart') then
03        TouchFunction(Bandit)
04    end
05end
06for _,Bandit in pairs(workspace.Bandits:GetDescendants()) do
07    AddBandit(Bandit)
08end
09workspace.Bandits.ChildAdded:Connect(function(Bandit)
10    AddBandit(Bandit)
11end)

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.

Ad
Log in to vote
0
Answered by 6 years ago

The problem is, you didn't use the debounce to see if the loop should run or not.

01for i,v in pairs(workspace.Bandits:GetDescendants()) do
02if v:IsA("BasePart") then
03local 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.
06TouchDebounce = true
07 local Hum = h.Parent.Humanoid
08local DamageAmount = Settings.BANDIT.DAMAGE dmgfunction(v, Hum, DamageAmount)
09wait(2)
10TouchDebounce = false
11end
12end)
13end
14end
0
Sorry if the code looks a bit weird. 222ono222 47 — 6y
Log in to vote
0
Answered by
OldBo5 30
6 years ago

Fix:

01--// Variables \\--
02local RunService = game:GetService("RunService")
03local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS)
04local Respawn_Time = Settings.BANDIT.RESPAWN_TIME
05local plrServ = game:GetService('Players')
06local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit
07 
08local function DamageEnemy(NPC, h, Damage)
09    h.Parent.Humanoid:TakeDamage(Damage)
10end
11 
12local function TouchFunction(Bandit)
13    local Debounce = true
14    local DamageAmount = Settings.BANDIT.DAMAGE
15    Bandit.Touched:Connect(function(h)
View all 29 lines...

You forgot the == true, I think. Or == false.

1
Actually simply saying `if Debounce then` is equal to if Debounce == true. Thanks for trying though BlackOrange3343 2676 — 6y
0
Oh. At least I know that now. Thanks! OldBo5 30 — 6y
0
np BlackOrange3343 2676 — 6y

Answer this question