I have made a function but because the script is big I'm not sure if it's working as intended.
The script is RPG and the function gets a monster's TILEX and TILEY and compares it with all players' TILEX and TILEY to see if they're in range.
The range is CONFIG.SPAWNDISTANCE which is static and equal to 128.
It will basically return true if the monster is within 128 X,Y tiles of any player or false if not.
This is an image I used if you want to take a look and the function is below it.
P > Player
M > Monster
X > TileX
Y > TileY
-+ > X smaller and Y bigger (and so on for the rest)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function InPlayerRange(tilex, tiley) 	local XDist, YDist = 0, 0 	for _,id in pairs(player(0,"tableliving")) do 		if (tilex > player(id,"tilex")) and (tiley > player(id,"tiley")) then -- ++ 			XDist = tilex - player(id,"tilex") 			YDist = tiley - player(id,"tiley") 		elseif (tilex < player(id,"tilex")) and (tiley < player(id,"tiley")) then -- -- 			XDist = player(id,"tilex") - tilex 			YDist = player(id,"tiley") - tiley 		elseif (tilex > player(id,"tilex")) and (tiley < player(id,"tiley")) then -- +- 			XDist = tilex - player(id,"tilex") 			YDist = player(id,"tiley") - tiley 		elseif (tilex < player(id,"tilex")) and (tiley > player(id,"tiley")) then -- -+ 			XDist = player(id,"tilex") - tilex 			YDist = tiley - player(id,"tiley") 		end 		if XDist <= CONFIG.SPAWNDISTANCE and YDist <= CONFIG.SPAWNDISTANCE then 			return true 		end 	end 	return false end