How to tell if a player is with one or more players with a local script

Alright, so, I’m just trying to figure out how to tell if the player is alone, and if they are, they begin to take damage.
I’ve looked all over devforums and I couldn’t find anything that could help me with this.
All i need is a simple script (maybe using magnitude between torsos) to get this outcome
Any help would be… helpful.

im sort of looking for something like

if player.Character.Torso.magnitude < 20 then
safe:FireServer()
else
unsafe:FireServer()

but how to get the magnitude… and then find the nearest player

1 Like

This should probably work.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")

local nearest = nil

for _,v in pairs(game.Players:GetChildren()) do
	local v_char = v.Character
	if not v_char then return end
	if v_char == char then return end
	local v_hrp = v_char:WaitForChild("HumanoidRootPart")
	local dist = (hrp.Position - v_hrp.Position).Magnitude
	if not nearest then
		nearest = dist
	end
	if dist < nearest then
		nearest = dist
	end
end

if nearest < 20 then
	print("safe")
end

2 Likes

thanks!!!

1 Like

Personally, I would do this on the client to avoid exploitation. The player could just delete/edit that script to not take damage. I thought this may be good to consider.

1 Like