Attempt to index nil with MoveTo()

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like to be able to get the character of the player and move it.

  2. What is the issue? Include screenshots / videos if possible!
    It gives me the following error: attempt to index nil with 'MoveTo'
    If I add the WaitForChild for the Character it would just start waiting for an infinite time.
    Script:

local Main = script.Parent.Parent
local Player = game.Players:FindFirstChild(Main.Name)
Player.Character:MoveTo(game.Workspace.Land.Position)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I can’t find one

The script probably isn’t under the player, which is why it’s not working. Could you provide the script that puts it under the character?

Make sure you’re actually determining the player. That’s the reason why you can’t find the character if the Player is not even an actual Player.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid: Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
		
		if Humanoid then
			print("Humanoid has been found! Moving the character to the target location")
			
			Humanoid:MoveTo(Insert_Target_Location_Here)
		else
			error("Humanoid not found")
		end
	end)
end)

Basically what this script does is that the script will trigger everytime a player has loaded their character. Then, find their humanoid to move to the “Insert_Target_Location_Here” (It can be either an instance or Vector3). Please keep in mind that this is just an example where it uses on ALL players.

Correct me if I’m wrong.

It isn’t under the player, it’s in the workspace

Not all players needs to be moved there. Only the found player

So why are you trying to move a player, when the script is under workspace, but you’re referencing the workspace as a player?

I’m trying to move the player from the workspace. I’m not referring the workspace as the player? what you mean

There are a few posts on the dev forum about this already! Some of those may help.

First, I don’t believe the character is actually a child of the Player. Something like CharacterAdded:Wait() is often used to wait for it to be added.

If you’re just trying to force match the character; I would use something that matches the current character model to the .Character of the player. Assuming that it’s not just a matching model name.

local Owner
for _,player in pairs(game:GetService("Players")) do
 if player.Character == script.Parent.Parent then -- assuming that script.Parent.Parent == a Character Model
  Owner = player
 end
end
if not Owner then error("Something else is wrong ig") end

Although I admit, I don’t see the usecase of what you’re doing so I may misunderstand. And as I read the posts while I type, I am farther confused.

local Main = script.Parent.Parent
local Player = game.Players:FindFirstChild(Main.Name)
Player.Character:MoveTo(game.Workspace.Land.Position)

This is your code. You’re getting “main” (what I assume is workspace), and then you’re finding a child in “player” with the name workspace, which will return nil; because most likely there isn’t a player named workspace. (Exactly why it’s erroring.)

1 Like

So what are you trying to accomplish? The player you’re trying to determine can’t be found on Workspace, but rather in Players.

local Players = game:GetService("Players")

wait wait let me explain my script to you two @CommanderRanking @Bellyrium

local Main = script.Parent.Parent

^ this line gets this part in the workspace. Why? cause it has the name of the player that needs to be moved with MoveTo()

local Player = game.Players:FindFirstChild(Main.Name)

^ This line finds the right player by the name of the instance, that it’s the player name.

Player.Character:MoveTo(game.Workspace.Land.Position)

^ This line should move the Player to the position, but gives me the error

Like I said earlier, you cannot find the player within the workspace. Instead, you’ll find the character of the player (or a model of the player).

The reason why you got nil is because there is no child named “Character” in Player as you stated in the script.

Well using game:GetService("Players") is usually safer.
Also, try wrapping it in if Player and Player.Character then and have an else statement to print something out if it fails.

Also, are you positive the part has the same name as the player? Uppercase can matter. Or if you changed the name through a local script, the server cannot see the change

^ The player gets found, and if I click the print with it, it shows me the player in game.players.

^ I will use the service then

^ How can I do in another way? I can’t move the script from there, it is needed for another thing.

Could you give a brief summary of your purpose? What exactly is the Main you stated in the script?

It’s the data container.
Basically Main is the folder that contains all the data i need for the game (Aka player coins, location ecc.)
The why i need it to be in the workspace it’s cause there are some values that have a script under it, and start on the value change (such as this case)
Let me send you a photo of it:
Screenshot (1239)
The location script is the one that I’m having problems with.

The full script without cut is the following:

--Variables
local Main = script.Parent.Parent
local Value = script.Parent
local Player = game.Players:FindFirstChild(Main.Name)

--Fuction
Value:GetPropertyChangedSignal("Value"):Connect(function()
	local New = Value.Value
	--Check
	if New == "GrassLand" then
		Player.Character:MoveTo(game.Workspace.GrassLand.Spawner.Position)
	end
end)

is this script waiting until the player FOR SURE has a character? They can load oddly
Maybe Pcall it?

If I add the "WaitForChild(“Character”) it will just give me an infinite wait. It basically can’t find the player character

I mentioned before, the Character is not a child but is within the meta…
A pcall will protect it and cause it to not break the script if it errors. So only when the character exists does it work.

.CharacterAdded() is an event of player you can wait for too

Idk how to write it in pcall. Idk what a pcall is.