Attempt to index nil with MoveTo()

it is on the dev forum! Theres even some explanation articles.

However

local character = player.Character or player.CharacterAdded:Wait() -- this tells it to wait for the character added even if player.Character is nil
character:MoveTo(Pos)

Should work but will stop the script waiting for a character

You should create a variable for the Character. It’s not sure whether if a character exist or not. Otherwise, it can error some time. Just like what @Bellyrium said. However, it will wait until the Character is found, causing the script to delay a bit.

1 Like

It seems to find the Character now, If i print the variable “character” it gives me the player name.
But it doesn’t move the character. It seems that it skip that line.

        local character = Player.Character or Player.CharacterAdded:Wait() 
		print(character) --Prints the player name
		character:MoveTo(game.Workspace.Part.Position) --Seems ignored
		print("Moved") --Gets printed

try adding a small delay before it moves? Also I think you might have to add something like
Character:WaitForChild("HumanoidRootPart").Anchored = false to assure the humanoid doesn’t mess with it much. Anchored parts can cause issues and I think it needs the root part to move.

– CFraming scripts and whatnot can also set a character back to where they came from. Is there anything else affecting the character movement?

1 Like

Seems that adding a delay doesn’t solve it.
There isn’t anything else affecting the player. I haven’t got to that part yet.

I’ve tried changing part to see if there was a problem with it, but it still ignores it with every part i could add as position.

I honestly don’t use :MoveTo() much… So I don’t really know what may be wrong. I am assuming you want the placement feature of the positioning instead of using a cframe?

The issue is happening because you are trying to access the Character property of the Player before it is available. The Character is loaded in asynchronously, so you need to wait for it before attempting to use it. You can do this by using a WaitForChild function:

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

it’s ok dude.
Yeah i would like it to go to the position, not by cframe.

local Main = script.Parent.Parent
local Player = game.Players:FindFirstChild(Main.Name)
local Character = Player.Character
repeat
wait()
until Character
Character:MoveTo(game.Workspace.Land.Position)
1 Like

so both of those are just replies without reading the thread…?

WaitForChild() Wont work…

I’ll test out :MoveTo() on a character. I think you might need to wait for the full appearance to load for a character too tbh!

player.CharacterAppearanceLoaded:wait() -- or something similar should wait for the character to be done loading and not just 'in the workspace'
-- can bug out though
1 Like

That may be why, let me see what happens if I add a longer delay

this line doesn’t work, but I added a delay of 3 seconds and it worked!!!
Thank you a lot for helping me out of this, and sorry for stressing you so much

it didn’t? Keep in mind that line should be added after - and not swaped out with - the other character wait.

Also, hiding spawn locations and setting a player.RespawnLocation can be very handy for what I think you’re doing!

Happy to help

1 Like

The error message “attempt to index nil with ‘MoveTo’” suggests that the value of Player.Character is nil . This means that Player does not have a character at the time that this script is run.

There are a few reasons why this might be the case:

  1. The player does not have a character yet. This can happen if the player has just joined the game and their character has not finished spawning yet.
  2. The player has left the game. In this case, the Player object will still exist, but it will not have a Character property.
  3. There is a typo in the name of the player. Make sure that Main.Name is the correct name of the player that you want to move.

To fix this issue, you can add a check to make sure that the player has a character before trying to move it:

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

You can also use WaitForChild to wait for the Character property to be added to the player object, but you should set a timeout to avoid waiting indefinitely:

local Main = script.Parent.Parent
local Player = game.Players:FindFirstChild(Main.Name)
local character = Player:WaitForChild("Character", 5) -- wait for up to 5 seconds
if character then
  character:MoveTo(game.Workspace.Land.Position)
end
1 Like

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