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?
[lua]
local math.random( 1, 50 )

local Sounds = {
Sound(“sound”),
Sound(“sound”),
Sound(“sound”)
}
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
``````

end
end

function IsFirst()
if ply:IsThere then
– code
end
end

function IsSecond()
if ply:IsFirst then
– code
end
end

function IsThird()
if ply:IsSecond then
– Code
[/lua]

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
return p2:GetPos():Distance(p1:GetPos()) < r
end[/lua]

To check if each player is in the radius you might need a hook in think
[lua]
– fill these in yourself
local PERSONAL_SPACE_TIME_LIMIT

local allPlayers = player.GetAll()
for i, p1 in ipairs(allPlayers) do
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
– 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
end
– if you haven’t checked the radius before
else
– set the time
end
– if you’re not in the radius but you’ve been in the radius