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

How do I have a part register getting touched once instead of repeatedly?

I'm wondering how I can have a part that only registers getting touched once and ends the code instead of having it repeatedly run the code every time it gets touched like I have below:

local health = script.Parent.Parent.Health.Value
function onHit(hit)
	health = health - 10
	print (health)
end
script.Parent.Touched:connect(onHit)

2 answers

✓ Accepted answer

You could use something like debounce, or make a table of players who already have touched the ting.

Debounce:

local health = script.Parent.Parent.Health.Value
local Debounce = false

script.Parent.Touched:connect(onHit)
if Debounce == false then
Debounce = true
health = health - 10
print (health)
wait(INSERT TIME UNTIL YOU CAN TOUCH THE PART AGAIN)
Debounce = false
end

local health = script.Parent.Parent.Health.Value
function onHit(hit)
health = health - 10
print (health)
end
script.Parent.Parent.Touched:connect(onHit)
script.Parent.Parent.Touched:connect(onHit)
wait(5)

1 comment

0

Horrible answer. wait(5) will just delay the damage

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