PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

DistanceFromCharacter

The DistanceFromCharacter Player function returns the distance between the character’s head and the given DataType/Vector3 point. It returns 0 if the player has no Player/Character.

This is useful when determining the distance between a player and another object or location in game.

##Note
If you would like to determine the distance between two non-player instances or positions, you can use the following:

local distance = (position1 - position2).magnitude

Parameters

Name Type Default Description

The location from which player’s distance to is being measured.

Returns

Return Type Summary

The distance in studs between the player and the location.


Code Samples


Measuring the Distance Between a Player and a Position

This example demonstrates how to measure the distance between a player’s Player/Character and another location.

This code will print the distance of each player’s character from the origin (0, 0, 0):

for _, player in pairs(game.Players:GetPlayers()) do
    print(player:DistanceFromCharacter(Vector3.new(0, 0, 0)))
end