Get player distances and check if its under a specific number with serverscript

Ive been trying to get the all players distance from an object, and check if one of those are smaller than a specific number. I dont even know how to go about this. Ive tried game.Players:GetPlayers() which returns a table but i dont know how to get the distances from those players in the table and the object, and find the player that is 20 studs away or less. Thank you!

Hi you can use .magnitude for this! This will allow you to get the distance between vectors and more. Learn more about .magnitude here: Vector3 | Documentation - Roblox Creator Hub

local Distance = 20

local Character --Character
local Part --Part

local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

if (HumanoidRootPart.Position - Part.Position).Magnitude <= Distance then
	print("Within distance!")
end
1 Like

First you need to get players character not the player, you can do this by:

local Players = game:GetService("Players")
local Player = Player:FindFirstChild("PUT_THE_NAME_OF_PLAYER_YOU_WANT_TO_USE")

local Character
repeat wait() Character = Player.Character until Character ~= nil --Waits till the players character is available

then get the relative distance and its magnitude

local RelativeDistance = (Part.Position - Character.HumanoidRootPart.Position)
local Magnitude = RelativeDistance

Here RelativeDistance is a vector3 value indicating distance and Magnitude is the variable that actually has the distance value in numbers, (Make sure you replace Part with actual Part you want the distance from)

1 Like

Is there any way to get all of the players via game.Players:GetPlayers() in a table and get the distances between the part and every single one of the players, then check if its smaller than 20 and get the player which has the distance smaller than 20?

1 Like

You can use for loops like this:

local players = game:GetService("Players")

for index, value in pairs(players:GetChildren)
    --All your code goes here, this code is repeated for each player and
        value variable is the player that changes every loop, so you want your all
            position detecting code here.
end
1 Like

If you have any questions let me know!

local Players = game:GetService("Players")

local Part --Put Part Here

local ClosestPlayer,ClosestDistance

for _,v in pairs(Players:GetPlayers()) do -- Loop through all players
	local Character = v.Character
	if not Character then continue end

	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")

	if not HumanoidRootPart then continue end --Character checking^

	local Distance = (HumanoidRootPart.Position - Part.Position).Magnitude --Get distance in studs between 2 vectors

	if not ClosestPlayer or ClosestDistance >= Distance then
		ClosestPlayer = v 
		ClosestDistance = Distance --Set closest player and distance^
	end
end

if ClosestDistance and ClosestDistance <= 20 then --Check to see if distance is less then 20
	print(ClosestPlayer,ClosestDistance)
else
	print("Nobody in range!")
end
2 Likes

Bro, don’t just give him complete code, he is here on the forums to get assisted, not to get his work done for free.

1 Like

Sorry that’s my fault, some people learn by reading through

1 Like

You aren’t making them learn by giving them pre-written code, you are spoon-feeding him the code so he will not be able to make his own stuff easily and will ask to others for code.

1 Like

You are very much right I apologize

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.