I have an Obstacle Course game on top of lava, and when I play the game, I get an error from the script that will kill the player if that player touches the lava, here is the script:
function onTouched(part)
local h = part.Parent:FindFirstChild("Humanoid")
if h~=nil then
h.Health = h.Health-100
end
end
script.Parent.Touched:connect(onTouched)
The error says this:
12:01:24.949 - Workspace.Lava.Script:2: attempt to index nil with ‘FindFirstChild’
How do I fix this? Thank you very much
function onTouched(part)
local h = hit.Parent:FindFirstChild("Humanoid")
if h~=nil then
h.Health = h.Health-100
end
end
script.Parent.Touched:connect(onTouched)
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
humanoid.Health = humanoid.Health - 100
end
end)
or
local function onTouched(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
humanoid.Health = humanoid.Health - 100
end
end
script.Parent.Touched:Connect(onTouched)
function onTouched(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h~=nil then
h.Health = h.Health-100
end
end
script.Parent.Touched:connect(onTouched)
function onTouched(hit)
local h = hit.Parent:FindFirstChild(“Humanoid”)
if h~=nil then
h.Health = h.Health-100
end
end
script.Parent.Touched:connect(onTouched)
You should use h.Health = 0 instead of h.Health = h.Health - 100
If you use h.Health = h.Health - 100, weird things could happen like the health bar constantly going into the negatives as the player is falling apart.