Is this check for nearby players efficient?

I’m unable to fully test this in game, as the game isn’t public, and studio server tests aren’t working for some reason, so can’t test if this even works

local PlayerInteractions = {}

local Players = game:GetService('Players')
local RunService = game:GetService('RunService')

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local function CheckDistance()
	for _, player in pairs(Players:GetPlayers()) do
		if player ~= Player then
			local OtherCharacter = player.Character
			if OtherCharacter then
				if Player:DistanceFromCharacter(OtherCharacter.HumanoidRootPart) <= 10 then
					print('You are near a player')


return PlayerInteractions


1 Like

This code functionally does as you intended! All’s good!

Now, since this is put in #help-and-feedback:code-review, I’m practically obliged to assume this isn’t a prototype, but something intended for production. Assuming this is the case: I’d advise you to modularize your code (Maybe a function called VectorUtil.distance and PlayerUtil.getOtherPlayers?), make your naming more consistent (all of your code uses PascalCase except for player, and perhaps use a linter (it will warn you that some variables are unused among other gotchas; you might already be using one)?

TL;DR: Yes! It works you brilliant programmer, you.