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

Why does this script damage me? (easy for experienced)

Asked by
Hypgnosis 186
6 years ago

I'm extremely new to scripting. I've been messing around and challenging myself to script certain stuff. Below is the code block, with some hopefully helpful comments.

Also, it's an edited version of the fire tutorial on the wiki.

My goal is to have my character be damaged when it touches the 'Lava1' part, taking 5 damage every second until it dies. And before my character touches the lava part, have it print "I am not on fire!"

wait(1) -- Waits for player to load in.

-- Variables for the lava, users, user HP, and checking to see if player has touched lava.
local Lava = game.Workspace.Lava1
local user = game.Players.LocalPlayer.Character.Humanoid
local userHP = game.Players.LocalPlayer.Character.Humanoid.Health
local userOnFire = Lava.Touched


-- Function that creates the fire effect.
function lightOnFire(part)
    print("Going to light this part on fire:")
    print(part.Name)

    local fire = Instance.new("Fire")
    fire.Parent = part
end

userOnFire:connect(lightOnFire) --calls upon function



-- The below code begins to damage me as soon as I launch the test.

-- Also, it doesn't stop even after I am dead.

-- However, it does not occur during my second life (after the character has re-spawned).
if userOnFire then
    repeat
        wait(1)
        user:TakeDamage(5)
    until 
        userHP <= 0
    else 
        print("I am not on fire!")
end
0
Is this in script or in local script? PlaasBoer 275 — 6y
0
What does the output shows you> royee354 129 — 6y
0
It's in script within the Lava1 part. The output shows no errors and prints the lightOnFire function correctly. 'print("I am not on fire!")' does not appear. Hypgnosis 186 — 6y

1 answer

Log in to vote
0
Answered by
PlaasBoer 275 Moderation Voter
6 years ago
Edited 6 years ago

If this is in local script and it is in workspace it won't work because local script are different that scripts

Read More http://wiki.roblox.com/index.php?title=API:Class/LocalScript

Not only that but game.Players.LocalPlayer.Character.Humanoid will only work in local script

local Lava = game.Workspace.Lava1
local userOnFire = Lava.Touched

-- Function that creates the fire effect.
function lightOnFire(part)
    print("Touched name "..part.Name)
    --The next code checks if the part parent has child Humaniod 
    --if it doest that fucntion will return nil
    local playerHumanoid = part.Parent:FindFirstChild("Humanoid")
    if playerHumanoid ~= nil then
        print("Going to light this part on fire:")

        local fire = Instance.new("Fire")
        fire.Parent = part
        --I remove the if because we know the part will be on fire
         repeat
            wait(1)
            playerHumanoid:TakeDamage(5)
         until 
            playerHumanoid.Health <= 0
    end
end

userOnFire:Connect(lightOnFire)

Forgot use Connect method.

This doesn't have debounce what that means that everytime the player touched the brick he gets the method of 5 damage every second so that builds up.

0
Try to go thruw the code to understand it. PlaasBoer 275 — 6y
Ad

Answer this question