How do i detect when the player leaves the hitbox after it enters is

so i am making a system that when the player enters the hitbox he gets damaged every 0.05 seconds. that works, but i cant seem to figure out how to detect when the player leaves the part. So how do i do that? here’s my code:

local Bone = script.Parent
local HitBox = Bone.Hitbox
local overlapParams = OverlapParams.new()
local players = game:GetService("Players")






while true do
	task.wait()
	local HitContent = workspace:GetPartBoundsInBox(HitBox.CFrame,HitBox.Size,overlapParams)
	local HitList = {}
	local LastdamagedPart = {}
	local KR = false
	local CanDamage = true
	for i, v in pairs(HitContent) do
		local TargetChar = v.Parent
		local TargetHum = TargetChar:FindFirstChild("Humanoid")
		local Player = players:GetPlayerFromCharacter(TargetChar)
		
	if TargetHum then
			if KR == true then
			local timer = 4
			local CountDown = 0.5

			if timer >0 then
				wait(CountDown)
				timer -= CountDown
				TargetHum:TakeDamage(1)
				end
				if timer == 0 then
					KR = false
				end
				
		end

		
			if Player.SoulsFolder.GettingDamaged.Value == true then return end
			if not table.find(HitList,TargetChar.Name) then
				table.insert(HitList,TargetChar.Name)
				
				Player.SoulsFolder.GettingDamaged.Value = true
				KR = false
			
				print(HitContent)
				TargetHum:TakeDamage(1)
				task.wait(0.05)
				Player.SoulsFolder.GettingDamaged.Value = false
				table.insert(LastdamagedPart,TargetChar.Name)
			elseif table.find(LastdamagedPart,TargetChar.Name) then
				KR = true
			
				

			end
			
		
				
			
				
				
			end
				
		

	end
	

	
	end




Maybe you can try putting a variable outside of the loop, and put the target inside that variable when the target enter the HitBox

Then you can check if the already-entered target is still inside the HitContent by using the variable. If it is not inside then that’s how you know that the target left the HitBox

1 Like

guys i found it i just checked if the player was in the table and if the players leftfoot/rightfoot (doesnt matter) and if the distance between the legs and hittbox was greater than 1.8 it activated the KR so here is the script. Here is the script:

local Bone = script.Parent
local HitBox = Bone.Hitbox
local overlapParams = OverlapParams.new()
local players = game:GetService("Players")


while true do
	task.wait()
	local HitContent = workspace:GetPartBoundsInBox(HitBox.CFrame,HitBox.Size,overlapParams)
	local HitList = {}
	local LastdamagedPart = {}
	local KR = false
	local CanDamage = true
	for i, v in pairs(HitContent) do
		local TargetChar = v.Parent
		local TargetHum = TargetChar:FindFirstChild("Humanoid")
		local Player = players:GetPlayerFromCharacter(TargetChar)
		
	if TargetHum then
			if KR == true then
			local timer = 4
			local CountDown = 0.5

			if timer >0 and TargetHum.Health >= 2 then
				wait(CountDown)
				timer -= CountDown
				TargetHum:TakeDamage(1)
				end
				if timer == 0 then
					KR = false
				end				
		end

		if Player then
			if Player.SoulsFolder.GettingDamaged.Value == true then return end
			if not table.find(HitList,TargetChar.Name) then
				table.insert(HitList,TargetChar.Name)
				
				Player.SoulsFolder.GettingDamaged.Value = true
				KR = false
			
				print(HitContent)
				TargetHum:TakeDamage(1)
				task.wait(0.05)
				Player.SoulsFolder.GettingDamaged.Value = false
				table.insert(LastdamagedPart,TargetChar.Name)
				end
				end
			if table.find(LastdamagedPart,TargetChar.Name) and (TargetChar["Right Leg"].Position - HitBox.Position).Magnitude >= 1.8 then
				
			KR = true
			else
				KR = false

			end															
		end
	end	
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.