Attempt to index nil with Position

So, I’m trying to use MoveTo for 9 NPC’s and the player without referencing Humanoid to imitate teleportation and I’m attempting to teleport everyone and the code fails beginning with the third teleportation:

local button = script.Parent
local seat = game.Workspace:WaitForChild("platform") 
local player = game.Players.LocalPlayer
local character = game.Players.LocalPlayer.Character
local humanoid = character:FindFirstChild("Humanoid")
local second = game.Workspace:FindFirstChild("plat2")
local offset = Vector3.new(0,5,0)
local begin1 = game.Workspace:FindFirstChild("Begin1")
local begin2 = game.Workspace:FindFirstChild("Begin2")
local begin3 = game.Workspace:FindFirstChild("Begin3")
local begin4 = game.Workspace:FindFirstChild("Begin4")
local begin6 = game.Workspace:FindFirstChild("Begin6")
local begin7 = game.Workspace:FindFirstChild("Begin7")
local begin8 = game.Workspace:FindFirstChild("Begin8")
local begin9 = game.Workspace:FindFirstChild("Begin9")
local begin10 = game.Workspace:FindFirstChild("Begin10")
local Velma = game.Workspace:FindFirstChild("Velma")
local Jack = game.Workspace:FindFirstChild("Jack")
local Christina = game.Workspace:FindFirstChild("Christina")
local Natalia = game.Workspace:FindFirstChild("Natalia")
local Julia = game.Workspace:FindFirstChild("Julia")
local Alex = game.Workspace:FindFirstChild("Alex")
local Fleur = game.Workspace:FindFirstChild("Fleur")
local Elias = game.Workspace:FindFirstChild("Elias")
local Albert = game.Workspace:FindFirstChild("Albert")
local playerscripts = game.Players.LocalPlayer: WaitForChild("PlayerScripts")
local PlayerModule = require(playerscripts: WaitForChild("PlayerModule"))
local controls = PlayerModule:GetControls()


local function onClick()
	controls:Enable()
	button.Parent.Visible = false 
	if character and humanoid and seat then
		Velma:MoveTo(begin1.Position + offset)
		Jack:MoveTo(begin2.Position + offset)
		Christina:MoveTo(begin3.Position + offset)
		Natalia:MoveTo(begin4.Position + offset)
		Julia:MoveTo(begin6.Position + offset)
		Alex:MoveTo(begin7.Position + offset)
		Fleur:MoveTo(begin8.Position + offset)
		Elias:MoveTo(begin9.Position + offset)
		Albert:MoveTo(begin10.Position + offset)
		player.Character:MoveTo(second.Position + offset)
	end
end




-- Connect the onClick function to the TextButton's MouseButton1Click event
button.MouseButton1Click:Connect(onClick)

Starting at Christina, the code fails and outputs an attempt to index nil with Position error for line 38 (Christina’s moveto() line), but here’s the confusing part: My references are correct and if I were to remove Christina’s line, it would say the error for Natalia.

Because of this, instead of moving all 10 models, I can only move the first two.


Note that, all the begins are Unions in the Workspace. Their parent is the Workspace itself.

Try using WaitForChild() when getting the begin variables. That is because the begin variables might not have loaded before the script started running, and since you are using FindFirstChild(), it would return nil in that situation. WaitForChild() will yield the script until it finds an object with the same name.

3 Likes

Spot on analysis, it worked, thank you.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.