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

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

Asked by 6 years ago

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

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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

Ad
Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago
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)
0
Horrible answer. wait(5) will just delay the damage iRexBot 147 — 6y

Answer this question