I'm trying to make a slow kill script to slowly kill a person when they are on the brick, but it stops damaging the person when they step off of it. Here's the code I have, it works but it doesn't stop when they step off the brick:
1 | function onTouched(hit) |
2 | local h = hit.Parent:findFirstChild( "Humanoid" ) |
3 | repeat |
4 | h.Health = h.Health - 0.2 |
5 | wait( 2 ) |
6 | until h.Health = = 0 |
7 | end |
8 |
9 | script.Parent.Touched:connect(onTouched) |
BasePart:GetTouchingParts() seems promising.
01 | local Players = game:GetService( "Players" ) |
02 | local Debounce = . 3 |
03 | local Damage = 5 |
04 | while wait(Debounce) do |
05 | local Connection = script.Parent.Touched:Connect( function () end ) |
06 | for _,Object in pairs (script.Parent:GetTouchingParts()) do |
07 | Connection:Disconnect() |
08 | local Rig = Object:FindFirstAncestorWhichIsA( "Model" ) |
09 | if Rig ~ = nil and Players:GetPlayerFromCharacter(Rig) ~ = nil then |
10 | local Humanoid = Rig:FindFirstChildOfClass( "Humanoid" ) |
11 | if Humanoid ~ = nil then |
12 | Humanoid:TakeDamage(Damage) |
13 | end |
14 | end |
15 | end |
16 | end |
You can simply do this:
1 | script.Parent.Touched:Connect( function (hit) |
2 | local hum = hit.Parent:FindFirstChild( "Humanoid" ) |
3 | if hum then |
4 | wait( 2 ) |
5 | hum:TakeDamage( 0.2 ) -- Gradual damage |
6 | end |
7 | end ) |
The reason why your script keeps going is because you're telling it to repeat the action even when the Humanoid stops making contact with the Part. (You should never do this.)