You can write your topic however you want, but you need to answer these questions:
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.
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)
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I can’t find one
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.
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.)
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
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:
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)
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