Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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())

1 comment

0

It is redundant to rename Fire as Fire because it's the default name. Also, the correct capitalization is "Name" not name.

3 answers

You should be only doing 1 Parent

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

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

1 comment

0

And it`s still not working

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())

1 comment

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.