Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

#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.

8 comments

0

Could you give a bit more information about what you're trying to do and what kind of script you're using?

0

I'm trying to make a claim button when you hatch a egg and click claim the element will go into players inventory

0

What line in the code you posted I sent the error on?

0

Line 34, so basically the if statement

0

what the hell kind of script is this?!

0

Damage script

0

Server script

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.

3 answers

✓ Accepted answer

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

1 comment

0

Sorry if the code looks a bit weird.

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.

3 comments

1

Actually simply saying if Debounce then is equal to if Debounce == true. Thanks for trying though

0

Oh. At least I know that now. Thanks!

0

np

This discussion is preserved as it was. New answers, comments, and votes are closed.