Detecting collision only once per part doesn't work

Hello Developers,

I’m currently working on a game and I’m hitting a problem.
I’ve tried searching everywhere on the forums, YouTube and Roblox developer pages but I can’t seem to find anything regarding my issue.
Any and all help is very much appreciated!

In my game there’s a certain part (this part can appear multiple times), this part has a basic collide script in it which detects if the NPC’s I made walk through it. This works fine, I made it so it only triggers upon colliding with the HumanoidRootPart and also doesn’t trigger while the effect is already working.

However, I want to add a “queue” like system to this script. So if 2 NPC’s walk through the part, it happens twice! (In a row not at once.)
This did not work the way I thought it would, I understand exactly why, but I don’t know what to do to actually get what I need.
Because, as I’m sure most of you could have guessed, now that I added this queue both NPC’s their HumanoidRootParts get added to the queue multiple times. Resulting in this effect happening 20 to 90 times in a row instead of twice in a row.

I have no clue how I can script it so both HumanoidRootParts only get added once, BUT can still be added to another version of this part if they then walk through that one.

This is the script:

local unit = script.Parent.Parent
function ot(hit)
if hit.Parent.Parent:IsA("Folder")then
--print(hit.Name)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if h.Health ~= 0 then
--print(h.Name)
					if hit.Parent.Player.Value ~= unit.Player.Value then
					if hit.Name == "HumanoidRootPart" then
						unit.InRange.Value = unit.InRange.Value+1
						if unit.Attacking.Value == false then
					unit.Attacking.Value = true
					--print(hit.Parent.Name)
-- HERE IS WHERE THE EVENT HAPPENS BUT REMOVED BECAUSE NOT IMPORTANT FOR FORUM
unit.InRange.Value = unit.InRange.Value-1
unit.Attacking.Value = false
end
end
end
end end end end
script.Parent.Touched:connect(ot)

I’m not sure if the script is of any use because my only real problem is that it runs too many times. The script itself (after many bugs) now works flawless.
If I place the “InRange” value after the “Attacking” value it won’t run more then once (and prevents the second NPC to run it/be added to the queue/InRange value) but then again, I don’t want it to run once, I want it to run once per unique unit that walked through.

I hope this is clear, if not, please ask! And thanks a lot in advance!

1 Like

For starters if it’s happening 20 to 90 times add a debounce to give it cooldown. Also try adding a bool value inside of the NPC once it hits the block so for example.

local Bool = Instance.new("BoolValue") --Setting up bool statement
    Bool.Name = IsHit
    Bool.Value = false

local unit = script.Parent.Parent
Debounce = false --Cooldown Statement
function ot(hit)
if Debounce == false then --Will run code below once Debounce is true
    Debounce = true
if hit.Parent.Parent:IsA("Folder")then
--print(hit.Name)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if h.Health ~= 0 then
--print(h.Name)
					if hit.Parent.Player.Value ~= unit.Player.Value then
					if hit.Name == "HumanoidRootPart" then
                    if hit.IsHit == nil then
                    Bool.Parent = hit--Putting bool value into Player or Object
						unit.InRange.Value = unit.InRange.Value+1
						if unit.Attacking.Value == false then
					unit.Attacking.Value = true
					--print(hit.Parent.Name)
-- HERE IS WHERE THE EVENT HAPPENS BUT REMOVED BECAUSE NOT IMPORTANT FOR FORUM
unit.InRange.Value = unit.InRange.Value-1
unit.Attacking.Value = false
else
end
end
end
end end end end 
 Debounce = false --Once script is over It'll reset the debounce
end
script.Parent.Touched:connect(ot)

I didn’t test the script but just let me know if it works.

1 Like

The debouncing kind of works. It now happens about 2 to 5 times per unit so that’s already a massive improvement.
But the bool value unfortunately doesn’t work. It’s ironic but it’s saying it can’t find “hit.IsHit” because it’s nil.
But if it would have worked I have to doubt it would work perfect because I think it would mean the effect won’t trigger again if the NPC then walks through a second part with this same script.

I still really appreciate the attempt.

1 Like

Ah I see well glad I change something :+1: