Why does the lava keep harming even when the player is out of it?

So, I was scripting my own lava because I could’nt find the lava I wanted anywhere in the Toolbox, But when I take the player out of the lava, it continues it’s harming effect.

Anyone got a solution? Here is my entire script:

local lava = script.Parent

local burnplayer = lava.LavaSettings.LavaBurnsPlayer.Value
local lavadelay = lava.LavaSettings.LavaDamageDelay.Value
local lavadamage = lava.LavaSettings.LavaDamageValue.Value

local onDelay = lava.LavaSettings.OnDelay.Value

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") 
while onDelay == false do 
		otherPart.Parent.Humanoid:TakeDamage(lavadamage)
		onDelay = true
		wait(lavadelay)
		onDelay = false
		end
	end


lava.Touched:Connect(onTouch)
6 Likes

It looks like your script loops forever. You may want to try using repeat until the player steps off of the lava.

2 Likes

I dont find anywhere it could fit, but thanks for the help.

1 Like

Instead of a loop try this:

local lava = script.Parent

local burnplayer = lava.LavaSettings.LavaBurnsPlayer.Value
local lavadelay = lava.LavaSettings.LavaDamageDelay.Value
local lavadamage = lava.LavaSettings.LavaDamageValue.Value

local onDelay = lava.LavaSettings.OnDelay.Value

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") 
if onDelay = false then
		otherPart.Parent.Humanoid:TakeDamage(lavadamage)
		onDelay = true
		wait(lavadelay)
		onDelay = false
		end
	end


lava.Touched:Connect(onTouch)
2 Likes

Pretty close, but only harms the player when moving in the lava.

1 Like

Sorry for late response, try this:

local OnGoingLoop = false

local function GetTouchingParts(part)
	local connection = part.Touched:Connect(function() end)
	local results = part:GetTouchingParts()
	connection:Disconnect()
	return results
end

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
	local char = otherPart.Parent
	if OnGoingLoop == false then
		OnGoingLoop = true
		while onDelay == false and humanoid do
			
			local damage = false
			local parts = GetTouchingParts(lava)
			
			for i,part in pairs (parts) do
				if damage == false then
					if part.Parent == char then
						damage = true
					end
				end
			end
			
			if damage then
				otherPart.Parent.Humanoid:TakeDamage(lavadamage)
				onDelay = true
				wait(lavadelay)
				onDelay = false
			else
				break
			end
		end
	end
end


lava.Touched:Connect(onTouch)

I used a technique by @/buildthomas, it might not be the most efficent code but I believe it should work.
EDIT: Added a variable so it doesn’t start a loop everytime .Touched is fired.

2 Likes

It did not work sadly.

Might turn it into a feature and add a flame particle on the players head

1 Like

Strange, it worked for me on studio, are there any errors? (BTW the variables are not there in the code I wrote)

2 Likes

There are no errors, I kept the old variables but for some reason it* does not work or show errors?

Edit: I turned off canCollide on the part

1 Like

This loop is problematic, it won’t cease at all, since the variable is false at all times after each exit.

Make it take damage when a Variable is true. Set the variable to true when .Touched is called, and set it to false when .TouchEnded is called

1 Like

Try not setting the variable of ondelay’s bool value and see how it goes.

local onDelay = lava.LavaSettings.OnDelay

local function onTouch(otherPart)
   local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") 
   if onDelay.Value == false do --no need a while loop here cus we only check for the bool and you made debounce cooldown!
	otherPart.Parent.Humanoid:TakeDamage(lavadamage)
	onDelay.Value = true
	wait(lavadelay)
	onDelay.Value = false
   end
end

Maybe, if you use some sort of local script you could possibly stop it. I really don’t know about this one.

I think a Region3 would be your to-go step for this.

You’ll constantly check all parts in that Region3, and if it’s a descendant of a player’s character, then damage him.

You could make a part in the air, that just reverses this “status effect” of the lava.