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/
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!
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)