How to stop a loop if player leave of a part

Hello everyone I maked a script that if player touches in the part he gets 10 of damage every 1 second but I want to make the player stop of be damaged if he leaves of the part.


Thank for all suport and sorry I can’t put scripts on cellphone.

1 Like

I don’t see the point of using a loop?
Player.Touched will fire everytime a player moves on it even when standing still so you can put a debounce outside of the function and check.

Also this will only work for one player but you could store the players userid inside of a table

local Debounce = false

local function onPartTouched(otherPart)
    if otherPart.Parent:FindFirstChildWhichIsA("Humanoid") then
      otherPart.Parent.Humanoid:TakeDamage(10)
      Debounce = true
      wait(1)
      Debounce = false
   end
end
1 Like

Oh sorry. Then there’s other way to make a player get damaged if he is inside of the part? and not touching in it. This is like a damage area.

Oh sorry I can’t seen your solution

It was my first time making it but don’t worry we all made mistakes sometimes in development.

Never mind try this my first code wasn’t really useful

local Debounces = {}

local function onPartTouched(otherPart)
	local Player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
	if otherPart.Parent:FindFirstChildWhichIsA("Humanoid") and Player and not Debounces[Player.UserId] then
		otherPart.Parent.Humanoid:TakeDamage(10)
		Debounces[Player.UserId] = true
		wait(1)
		Debounces[Player.UserId] = nil
	end
end
1 Like