Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I prevent this value from stacking?

Asked by 7 years ago

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.

3 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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)
0
Explain your answer in English. Don't just post code; this doesn't help the poster or the community understand why the code does what it does. hiimgoodpack 2009 — 7y
1
There, I explained it, now can you please stop bossing me around? Inconcinnus 90 — 7y
0
https://scriptinghelpers.org is bossing you around, since I GOT THAT SENTENCE FROM THEIR GUIDELINES. hiimgoodpack 2009 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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!

0
Ok, so it seems that I don't understand what milliseconds are, as half a second is exactly what I wanted. This solves my second problem, but it still doesn't solve the main problem. The amount of health loss still increases for each part on fire. Also, after a while, the script returns an error: 15:16:17.084 - Workspace.FirePart.Script:11: attempt to index field 'Parent' (a nil value) SparkingFruitPunch 4 — 7y
0
My edits should work now. hiimgoodpack 2009 — 7y
0
It's the same result as the previous time. Maybe I should've let you know that the error only shows up if the player dies due to the fire. SparkingFruitPunch 4 — 7y
Log in to vote
0
Answered by 7 years ago

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.

Answer this question