Attempt to Index Nil with CFrame

Hello, I am trying this function to teleport you if you have done the tutorial but it keeps saying,
ServerScriptService.Tutorial.SetGuide:16: attempt to index nil with 'CFrame ;any help?

	local function didTutorial()
		local character = players.Character
			local LowerTorso = character:WaitForChild("HumanoidRootPart")
			if players.MapWorld.Value ~= 0 then
				local MapArea = game.Workspace.Maps:FindFirstChild(players.MapWorld.Value)
				LowerTorso.CFrame = MapArea.CFrame
			end
		end

It seems like either LowerTorso or MapArea are nil since that’s where .CFrame is being used. Try adding print(LowerTorso, MapArea) before the CFrame’s set to see which one’s nil.

1 Like

How would I be able to find out which one is nil and how to fix it?

Add a print statement somewhere here

if players.MapWorld.Value ~= 0 then
    local MapArea = game.Workspace.Maps:FindFirstChild(players.MapWorld.Value)
    print(LowerTorso, MapArea)
    LowerTorso.CFrame = MapArea.CFrame
end

If LowerTorso isn’t nil, then HumanoidRootPart will be printed.
If MapArea isn’t nil, then the name of the map area will be printed.

Finding out which one is nil will let you know where the problem lies (either in the HumanoidRootPart or the map area not being retrieved).

Edit: the print(...) should be after MapArea is defined.

1 Like

If LowerTorso was nil it would have a yielding error trying to WaitForChild() it. Looks to me that it’s MapArea.
This can be solved by making sure MapArea exists before referencing it:

	local function didTutorial()
		local character = players.Character
			local LowerTorso = character:WaitForChild("HumanoidRootPart")
			if players.MapWorld.Value ~= 0 then
				local MapArea = game.Workspace.Maps:FindFirstChild(players.MapWorld.Value)
                if MapArea then
				      LowerTorso.CFrame = MapArea.CFrame
                else
                      print("MapArea doesn't exist!")
                end
			end
		end
2 Likes

I went ahead and used Character:MoveTo() and it worked.