I have a part that lights other parts on fire, and decreases health if the part that touched it belongs to a player.
local firepart = script.Parent firepart.Transparency = 1 local function LightOnFire(part) local fire = part:FindFirstChild("Fire") if not fire then fire = Instance.new("Fire") fire.Parent = part local hum = part.Parent:FindFirstChild("Humanoid") while fire do if hum then hum.Health = hum.Health - 1 end wait(.50) end end end firepart.Touched:connect(LightOnFire)
The effect that I want is to decrease the players health by a set value of 1 every 50 milliseconds, but that value adds up for every piece of the player that is on fire.
I'm sure you've noticed another problem. The player still loses health even when the fire is removed. I know, this whole block of code is a mess, now how would these problems be solved? Please keep in mind that I would like for normal parts to catch on fire too.
I think this should work.
local firepart = script.Parent firepart.Transparency = 1 local function LightOnFire(part) local fire = part:FindFirstChild("Fire") if not fire then fire = Instance.new("Fire") fire.Parent = part local hum = part.Parent:FindFirstChildOfClass("Humanoid") -- I made it 'local hum = part.Parent:FindFirstChildOfClass("Humanoid") so if it's renamed, it still finds the humanoid. while fire do if hum then hum.Health = hum.Health - 1 end wait(.5) ` firepart.TouchEnded:connect(function(part) --When the player steps off of the part, stop it from burning, this is the only new function I added if part.Parent:FindFirstChildOfClass('Humanoid') then break end end) end end end firepart.Touched:connect(LightOnFire)
The reason is, that the value only gets defined once. If fire gets removed, the value will not update. We can fix this by doing
local firepart = script.Parent firepart.Transparency = 1 local function LightOnFire(part) local fire = part:FindFirstChild("Fire") if not fire then fire = Instance.new("Fire") fire.Parent = part local hum = part.Parent:FindFirstChild("Humanoid") while part:FindFirstChild("Fire") and part.Parent:FindFirstChild("Humanoid") and part.Parent ~= nil do hum.Health = hum.Health - 1 wait(0.5) end end end firepart.Touched:connect(LightOnFire)
I also adjusted some stuff to shorten the code.
Hope this helps!
Okay so I managed to solve the problem involving the error appearing in the output:
local firepart = script.Parent firepart.Transparency = 1 local function LightOnFire(part) local fire = part:FindFirstChild("Fire") if not fire then fire = Instance.new("Fire") fire.Parent = part -- Sets any part that touches it on fire. local hum = part.Parent:FindFirstChild("Humanoid") if hum then -- If the part belongs to a Player, it will run the code below it. while part:FindFirstChild("Fire") and part.Parent:FindFirstChild("Humanoid") and hum.Health ~= 0 do -- I figured the error appeared because the script was still trying to reduce the players health even though it was at zero, so I set the loop to stop once the player's health was depleted. hum.Health = hum.Health - 1 wait(.50) end end end end firepart.Touched:connect(LightOnFire)
Two problems have been taken care of, but the MAIN problem has yet to be solved.