Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would get a players' location from a global script?

I am trying to get the players' location using a global script, this is what I have written:

local location = Vector3.new(0,0.5,0)
local playerName = script.Parent.Parent.Parent.Parent.Name
local playerLocation = game.Workspace.playerName.UpperTorso.Position

print(playerLocation)

I don't know why but it keeps saying that playerName is an invalid member of Workspace. Should I put a wait so that ROBLOX has time to load the character? What do I do? Thank you/

3 answers

You can use the PlayerAdded event and then wait for the player's character like so:

game:GetService("Players").PlayerAdded:Connect(function(player)
	local character = player.CharacterAdded:wait()
	local position = character:WaitForChild("HumanoidRootPart").Position
end)

If this answers your question, please accept this as the best answer!

1 comment

0

Thanks for posting this! I was able to get this to work by replacing the '.' with ':' after 'game.' Just adding a little context; the reason you have to use the 'Players' service and wait for an event is because your player is not initially in the game, so you have to wait for the event to fire before getting your position.

Put the Global Script in Workspace. Then try this code:

game.Player.PlayerAdded:Connect(function(Plr) -- Player is added
Plr.CharacterAdded:Connect(function(Char) -- Players' character added

local location = Char:WaitForChild("UpperTorso").Position -- wait for uppertorso to appear then get the position

end)
end)

Change the code to

local location = Vector3.new(0,0.5,0)
repeat wait()

until script.Parent.Parent.Parent.Parent.Character
local playerName = script.Parent.Parent.Parent.Parent.Name
repeat wait()

until game.Workspace.playerName.UpperTorso
local playerLocation = game.Workspace.playerName.UpperTorso.Position

print(playerLocation)

1 comment

0

playername wasn't valid again

This discussion is preserved as it was. New answers, comments, and votes are closed.