Radius of player

How do I make it so if players are around other players for a certain amount of time they will have a function executed on them.

Incoperated into this script so that when a player with IsFirst is around another player, other player will execute IsFirst

Also if IsFirst is executed on the next person (not the first person) will it not work since it requires IsThere to be ran?
local math.random( 1, 50 )

local Sounds = {
local rndmsnd = math.random(1, 3)
local randomSound = (Sounds[rndmsnd])

function IsThere()
local plays = player.GetAll()
local newpl = plays[math.random(1, #plays)]

do IsFirst()
return newpl


function IsFirst()
if ply:IsThere then
– code

function IsSecond()
if ply:IsFirst then
– code

function IsThird()
if ply:IsSecond then
– Code

The do IsFirst() … end isn’t required.
Syntaxically ply:IsFirst doesn’t make any sense. First of all you haven’t called the function, which means it would be ply:IsFirst(), secondly the player doesn’t have an IsFirst function, you’d need to find the metatable if you wanted to do it like that, third the IsFirst function doesn’t take any parameters, so even if you got it into the player table doing ply:IsFirst() would have the same result as ply.IsFirst() and simply IsFirst()

Here’s a function to tell if two players are in a radius.
[lua]-- p1 is the first player, p2 is the second and r is the radius they must be in
local function playersInRadius(p1, p2, r)
return p2:GetPos():Distance(p1:GetPos()) < r

To check if each player is in the radius you might need a hook in think
– fill these in yourself

local inRadiusStart = {}
local allPlayers = player.GetAll()
for i, p1 in ipairs(allPlayers) do
inRadiusStart[p1] = inRadiusStart[p1] or {}
for j, p2 in ipairs(allPlayers) do
if j > i then – so we don’t repeat pairs of players
– if you’re in the radius
if playerInRadius(p1, p2, PERSONAL_SPACE_RADIUS) then
– and you’ve already checked the radius before
if inRadiusStart[p1][p2] then
– check if you’ve been in there too long
if CurTime() > inRadiusStart[p1][p2] + PERSONAL_SPACE_TIME_LIMIT then
– do whatever if they’re together too long
– if you haven’t checked the radius before
– set the time
inRadiusStart[p1][p2] = CurTime()
– if you’re not in the radius but you’ve been in the radius
elseif inRadiusStart[p1][p2] then
– unset the time to say you’ve left the radius
inRadiusStart[p1][p2] = nil
There’s probably a few optimizations that could be done and ideally the inRadiusStart table and subtables should have weak keys, but it should do what you’re asking for.

I think you ought to learn how to program in general before you program in garrysmod.

So the hook will affect ALL players, or only the player that has been randomly selected? I basically want it to spread with players contagiously and not just affect every player from the start, thanks for helping me regardless of how stupid I am :v: