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())
You should be only doing 1 Parent
local hum = hit.Parent:FindFirstChild ("Humanoid")
hit.Parent.Parent
is referring to the workspace
NOT a player
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())
I think you have to remove the space between the findfirstchild and the () thing