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

Why won't this fire script work?

Asked by 9 years ago

This is supposed to make the player turn on fire and they will start losing health. But this fire script won't work. This script is in a brick:

function loseHealth(part)
    print("Gotta set this part on fire!")
    print(part.Name)
    fire = Instance.new("Fire")
    fire.Parent = part
    --Now losing health...
    for i = 100, 0, -1 do
        wait(0.1)
        part.Parent.Humanoid.Health = i
    end
end

Most of this is in the wiki, so why isn't this right?

0
What isn't right about it? Is there an error? Does the script not run? ClassicTheBlue 65 — 9y
0
Oh wow, you're back. I actually haven't tested it, but I still don't believe it works. NeonicPlasma 181 — 9y
0
Try using this script in a brick with the Touched event. I forgot to put that on. NeonicPlasma 181 — 9y
0
Oh and also, I have some feedback to your game. You could hide secret codes in the lobby and when they enter one of the correct codes they get something to help them on the course. That is what I am working on. NeonicPlasma 181 — 9y

1 answer

Log in to vote
5
Answered by 9 years ago
  1. You forgot to hook up the function to an event
  2. Your code can run multiple times simultaneously
  3. The function might run on something that's not a player, thus breaking it entirely.
local burning = { }

function burnPlayer(player)
    -- If the player is already burning, then stop the function.
    if burning[player] then
        return
    end

    -- Set that the player is burning in our table, using the player object as an index.
    burning[player] = true

    -- Create the fire in the character's head.
    Instance.new("Fire", player.Character.Head)

    -- Set a variable to the Humanoid for easy reference.
    local Humanoid = player.Character.Humanoid

    -- Slowly remove player's health.
    for i = 100, 0, -1 do
        wait(0.1)
        Humanoid.Health = i
    end

    -- Make sure the player actually dies.
    player.Character:BreakJoints()

    -- Remove the player key from the table, so he can touch it again and burn to death after he respawns.
    burning[player] = nil
end

function PartTouched(part)
    -- Get the Player object from the character. If the part's parent isn't a character, this will be nil.
    local player = game.Players:playerFromCharacter(part.Parent)

    -- Check if player is nil. If it is, stop the function.
    if not player then
        return
    end

    -- Moving the burn player code into another function.
    burnPlayer(player)
end

script.Parent.Touched:connect(PartTouched)
Ad

Answer this question