Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Region3 Player detector is inconsistant?

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

1 answer

✓ Accepted answer

This could be due to the maximum amount of parts that the region can detect is 20 (by default). Simply just change line 7 to this:

local parts = workspace:FindPartsInRegion3(region, nil, math.huge) --region, ignoreDescendantsInstance, maxParts

3 comments

0

ah, if only i knew this. ill try it and update you to see if it works

0

thank you! i shoulda knew this earlier. i will keep note of this

0

No problem! Glad it worked!

This discussion is preserved as it was. New answers, comments, and votes are closed.