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

[ANSWERED] how to fix attempt to index nil with parent?

Asked by 2 years ago
Edited 2 years ago
local trigger = script.Parent

function onTouch(char)
    local hum = char.Parent:FindFirstChild("Humanoid") --error here
    if hum then
        hum.Parent.HumanoidRootPart.CFrame = workspace.Spawn.CFrame + Vector3.new(0, 10, 0)
    end
end

trigger.Touched:Connect(onTouch()) -- here was the mistake

-- please help on line 4 it says attempt to index nil with parent
-- lol i accidentally added an extra set of parentheses on the last line
0
LOL I JUST REALIZED I PUT AN EXTRA SET OF PARENTHASES IN THE .TOUCHED EVENT but thanks for your time man really appreciate it rokkufera 6 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

This means that char.Parent doesn't exist, and you are trying to find a humanoid (this isn't exactly what it means but you get the idea.)

Also make sure that any surrounding parts are anchored (and you want them to be anchored) since .Touched only interacts with unanchored parts.

local trigger = script.Parent

function onTouch(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    local hum = hit.Parent:FindFirstChild("Humanoid")
        hum.Parent.HumanoidRootPart.CFrame = workspace.Spawn.CFrame + Vector3.new(0, 10, 0)
    end
end

trigger.Touched:Connect(onTouch)

And if this doesn't work, or you want it to work just for players and not NPCs (if you have NPCs in your game), you can check if it's a player as well.

local trigger = script.Parent

function onTouch(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
            hum.Parent.HumanoidRootPart.CFrame = workspace.Spawn.CFrame + Vector3.new(0, 10, 0)
    end
    end
end

trigger.Touched:Connect(onTouch)
0
How did you make those boxes around the property/event wwwaylon09 113 — 2y
0
using tildas around that area, like this: `.Touched` FirewolfYT_751 223 — 2y
Ad

Answer this question