I have a script that is supposed to detect players that walk into a certain area. The script displays the number of players on a GUI. The script seems to work perfectly when one player walks into the zone. However, when more players walk into it, the number of players fluctuate, especially when the players move around in the zone. For example, if there are 2 players in the zone, the number will fluctuate from 2 to 1. I'm guessing this has to do with the script having difficulties detecting the other players. How do I fix this?
local region = Region3.new(Vector3.new(-35.75, 45.35, 74.25), Vector3.new(0.75, 63.85, 111.75))
local players = game:GetService("Players")
function getPlayersInRegion()
local t = {}
local parts = workspace:FindPartsInRegion3(region)
for _, v in pairs(parts) do
local plr = players:GetPlayerFromCharacter(v.Parent)
if plr and not table.find(t, plr) then
table.insert(t, plr)
end
end
return t
end
while true do
wait()
script.Parent.players.SurfaceGui.Frame.TextLabel.Text = #getPlayersInRegion()
script.Parent.playersval.Value = #getPlayersInRegion()
end

3 comments
ah, if only i knew this. ill try it and update you to see if it works
thank you! i shoulda knew this earlier. i will keep note of this
No problem! Glad it worked!