Lava part not working properly

So recently i’ve been trying to make a lava part that’ll constantly damage the player if they’re on the lava part. But for some reason after the player walks off the lava part, it will still constantly damage the player until the player is dead. After the player dies and they walk back onto the part it will not damage the player at all, rendering it useless after the player dies. Is there a way I can fix this?

Here is the script:

local Hitbox = script.Parent
local Damaging = false
local Touching = false
local Max = 5

Hitbox.Touched:Connect(function(hit)
	local Hum = hit.Parent:WaitForChild("Humanoid")
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Distance = Player:DistanceFromCharacter(Hitbox.Position)
	
	Touching = true
	
	print(Touching, Distance)
	
	while Touching do
		if Touching == true and Damaging == false then
			if Distance <= 10 then
				Damaging = true
				Hum.Health -= 5
				Damaging = false
			else
				Damaging = false
				Touching = false
				break
			end
		else
			break
		end
		task.wait(0.5)
	end
end)

Any help is appreciated!

You only checked the Distance once which is when the player touched it but isn’t recalculated in the loop. Also, you’re using one variable to store the “Touching” and “Damaging” for all the players which could be problematic

1 Like

as sata5pa3da said.

Correct script could be something like:

local Hitbox = script.Parent
local Damaging = false
local Touching = false
local Max = 5

Hitbox.Touched:Connect(function(hit)
	local Hum = hit.Parent:WaitForChild("Humanoid")
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

	Touching = true


	while Touching do
		local Distance = Player:DistanceFromCharacter(Hitbox.Position)
		if Touching == true and Damaging == false then
			if Distance <= 10 then
				Damaging = true
				Hum.Health -= 5
				Damaging = false
			else
				Damaging = false
				Touching = false
				break
			end
		else
			break
		end
		task.wait(0.5)
	end
end)
1 Like

so this did work but the Damage is very inconsistent and sometimes doesn’t work. I’ll show a video if it

here’s the video of what i meant

Hmm, thats weird it works pretty well for me.
Try this and check output

local Hitbox = script.Parent
local Damaging = false
local Touching = false
local Max = 5

Hitbox.Touched:Connect(function(hit)
	print(hit.Parent)
	local Hum = hit.Parent:WaitForChild("Humanoid")
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

	Touching = true


	while Touching do
		local Distance = Player:DistanceFromCharacter(Hitbox.Position)
		if Touching == true and Damaging == false then
			if Distance <= 10 then
				Damaging = true
				Hum.Health -= 5
				Damaging = false
			else
				Damaging = false
				Touching = false
				break
			end
		else
			break
		end
		task.wait(0.5)
	end
end)
1 Like

I tried to use these for reference but they never really helped out nor did they work. But I appreciate your help either way

If they did not work why were the marked as the solution in the linked threads?

doesn’t necessarily means it works. Then again It doesn’t really do what I’m trying to do specifically either way