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

Fire script don't working. "attempt to index nil with 'Parent'" What should i do to fix it?

Asked by 3 years ago

I want to make script for bonfire which will light a player character but always get problem Workspace.Part.FIRE:5: attempt to index nil with 'Parent'

Here is my script:

local bon = script.Parent

local function onTouch(hit)

    local hum = hit.Parent.Parent:FindFirstChild ("Humanoid")     -- here is problem
    if hum then     
        local Fire = Instance.new ("Fire", hit)
        Fire.name = "Fire"
        Fire.Enabled = true
        local Firedmg = 2
        while Fire.Enabled and hum.Health > 0 do
            wait (0.3)
            hum:TakeDamage (Firedmg)
            Firedmg = Firedmg + 0.5
        end
        local Firedmg = 2
    end
end

bon.Touched:Connect(onTouch())

0
It is redundant to rename Fire as Fire because it's the default name. Also, the correct capitalization is "Name" not name. Xapelize 2658 — 3y

3 answers

Log in to vote
1
Answered by 3 years ago

You should be only doing 1 Parent

local hum = hit.Parent:FindFirstChild ("Humanoid")

hit.Parent.Parent is referring to the workspace NOT a player

0
And it`s still not working ednerok0end 5 — 3y
Ad
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

At line 20, do

bon.Touched:Connect(onTouch)

You put the brackets on it, which meaning you just defined the variable as blank. You overlapped the HIT parameter and replaced it to nil. Nil does not have a Parent, which cause an error. What are you going to do is remove the brackets so the returned parameter is the default parameters.

TheBigBro122 is also right. Hit parameter returns bodyParts, and bodyParts parent is the character itself. Character parent are workspace, which also cause an error.

Heres is the improved script: I improved it myself. The script is better and clean:

local bon = script.Parent

local function onTouch(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum == true then
        local Fire = Instance.new ("Fire", hit)
        Fire.Enabled = true
        local Firedmg = 2
        while Fire.Enabled == true and hum.Health > 0 do
            wait (0.3)
            hum:TakeDamage(Firedmg)
            Firedmg += 0.5 -- Short way
        end
        -- local Firedmg = 2 -- Not needed (Delete if you want)
    end
end

bon.Touched:Connect(onTouch())
0
i already fix the problem with toolbox codes. I tried your code, but it not working, maybe i did a mistake in exlorer tree? I was making this code according to this video https://www.youtube.com/watch?v=MQS0Yjeb7r4 ednerok0end 5 — 3y
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I think you have to remove the space between the findfirstchild and the () thing

Answer this question