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 7 years ago
Edited 5 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):

--// 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,

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 — 7y
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 — 7y
0
What line in the code you posted I sent the error on? Creeperman1524 120 — 5y
0
Line 34, so basically the if statement BlackOrange3343 2676 — 5y
View all comments (4 more)
0
what the hell kind of script is this?! NsNidPL 41 — 5y
0
Damage script BlackOrange3343 2676 — 5y
0
Server script BlackOrange3343 2676 — 5y
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 — 5y

3 answers

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

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.

Ad
Log in to vote
0
Answered by 5 years ago

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
0
Sorry if the code looks a bit weird. 222ono222 47 — 5y
Log in to vote
0
Answered by
OldBo5 30
5 years ago

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.

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

Answer this question