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)
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
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)
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)