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

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

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

1 comment

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

1 answer

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)

2 comments

0

How did you make those boxes around the property/event

0

using tildas around that area, like this: .Touched

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