I want to make a script that plays an audio and takes -20 damage to the player while they’re touching the part. The code works just how I want it to, but it continues to work even after the player leaves the part and only ends when they die.
part = script.Parent
sound2 = script.Parent["scp cough 2"]
sound1 = script.Parent["scp cough 1"]
local debounce = false
local function onPartTouched(other)
if debounce == true then
return
end
debounce = true
local hum = other.Parent:FindFirstChild("Humanoid")
while true do
task.wait(1)
sound1:Play()
print("sound1")
task.wait(2)
sound2:Play()
print("sound2")
task.wait(1)
hum.Health -= 20
print(hum.Health)
if hum.Health == 0 then
break
end
if part.Touched == false then
debounce = false
end
end
debounce = false
end
part.Touched:Connect(onPartTouched)
Any help is greatly appreciated.