0
\$\begingroup\$

In Roblox Studio, I have this code in a script file:

while true do
    wait(0.01)
    local PlayersService = game:GetService("Players")
    local players = PlayersService:GetPlayers()
    for i, player in pairs(players) do
        print(player.Name)
    end
end

This successfully prints the player's name many times per second. However, I would like to know how I can get other properties, such as the player's position. player.Position doesn't work, and I have no idea what options are available because autocomplete is not working when I type player.. What properties are available on player and how do I get the position?

\$\endgroup\$
3
\$\begingroup\$

I feel your pain. Roblox is very confusing, despite their claims that it is an easy way to learn scripting.

To get a player's position (server side) you need to access the player's character property. (player.Character).

Before you can access the Character property, you need to get the player's object. This can be done be capturing the player object when they first load onto the server using a regular script somewhere in the Workspace.

The game I'm working on is a single player game, so I store the player object in an object container which I access whenever I need the value in any of my server side scripts.

For example:

game.Players.PlayerAdded:Connect(function(player)

workspace.Data.Player.Value = player

end)

This script fires when a player joins the game. In my case, only one player is on the server, but you can adapt the code to track other players as you see fit.

'Data' is just a folder I put in the Workspace and 'Player' (Capital P) is an ObjectValue container I named 'Player' which I use to store the player object. You can rename or adjust that any way you see fit, or store the player object in any manner you wish. Now if you want to access the player's properties, including position, you would do this in a regular script:

local player = Workspace.Data.Player.Value --Get's player object and stores it in 'player' variable

local var = player.Character.UpperTorso.Position -- Gets vector3 position

Or, If you want to get the individual X, Y, Z co-ordinates

local varX = player.Character.UpperTorso.Position.X

local varY = player.Character.UpperTorso.Position.Y

local varZ = player.Character.UpperTorso.Position.Z

There are different ways of getting the player object, but this is the way that I am using for my purposes.

Also, UpperTorso is used for R15 Humanoids, so it may not work on Humanoid models that are not R15. UpperTorso is just one of the humanoid body parts, which is used to track player position.

If you want to track a different body part, run your game in the Developer Studio, and while it is running, click on StarterPlayer and then HumanoidDefaultBodyParts (found in Explorer view) which will give you a list of trackable body parts you can use instead of UpperTorso. The only way to track a player position is through a body part.

Hope this helps, and good luck.


PS: https://developer.roblox.com/en-us/api-reference/class/Player

The above link will take you to a developer page. If you look at the far right, you will see a list of properties and events that are used in conjunction with the player object.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This is a great answer - Thank you for contributing this in such a well-meaning way \$\endgroup\$ – Natalo77 Apr 15 at 13:11
0
\$\begingroup\$

Sometimes Roblox studio actually doesn't show your options. I have experienced this before. This is due to the fact that the object isn't actually present in the workspace but will be created when the game starts. So, treat it like any other object.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ It doesn't look like this part of the question has been answered here: "What properties are available on player and how do I get the position?" \$\endgroup\$ – DMGregory Apr 10 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.