Lets get straight to the point, Does anyone know how to detect when a player leaves the region3 area, I’ve been trying to reset debounce values when the player leaves the region3 area with no success, if anyone knows how to detect, make it would help me alot.
I think at that point you should use a public module such as
Which features events for when a player enters and leaves a zone
If you still wanna do it yourself
Have a table set up for old players that were detected
Every time you check the region3, check the player sthat were found in it via another table and then compare with t he old table if it exists, if any of the players recently found were in the old table but not anymore, then they left
It’s a bit confusing for Region3s, although my method is probably not the best
Typically the best way to go about this is using .Magntidue checks on a loop, this will let you know if a player is in a zone, and by having a table of all in-zone players you will be able to detect which one leaves.
Example in psuedo code:
While running, and delaying do
loop through each player, if they are within range (magnitude check) and not in PlayerList, add to Playerlist.
Else if they are not in range, check if they are in PlayerList. If they are in the list, they have Left the region they were in, so remove them from the table and run whetever function you need to when they leave
Magnitude checks are fairly easy, here is an example where I get the closest player to a set point within a confined range (Im using a constant stud range, but you can make it within the range of a part or the distance between two parts which is close to what you want I think)
Your code with Magnitude checks can be as simple as you want, just gotta look up a few math formulas when you get into the more complicated things
function UtilFunctions:NearestPlayerFromPoint(point, range, ogPlr)
local Players = game:GetService("Players")
local closest = math.huge
local closestPlr -- make sure is not original player
for i, player in pairs(Players:GetPlayers()) do
if player ~= ogPlr then
local character = player.Character
local rootPart = character and character:WaitForChild("HumanoidRootPart")
local rootPos = rootPart and rootPart.Position
if character and rootPart and rootPos then
local distance = (point - rootPos).magnitude
distance = math.abs(distance)
print(player.Name.." Is ".. tostring(distance).." studs away")
if distance < closest and distance < range then
-- get LOS
closest = distance
closestPlr = player
end
end
end
end
if closestPlr then
return closestPlr
else
return nil
end
end
Ive tried using Magnitude b4, but switched to region3, im trying to keep it on region3, Client sided aswell, Thanks tho ill look if i can fix my code with what you sent me.
In which case, you dont have to use magntude, the other thins stay the same:
If player is in the region3, add them to a list of all players inside the region if they arent already there.
If a player is not in the region, but you find them (table.find) in the list, then you know they were in it last iteration, but have since left. Its best to keep a fairly short delay in your iteration when doing it this way. Good luck!
Alright, Thanks, Could u give me a example on the checking if players on in a table, (table.find), Never really messed with tables, if u could, if not then its g.
Sure, let “InRange” Be a table with all players in range already there.
This snipet of code will be after you confirmed the player was not in range:
if table.find(InRange,Player) then -- table.find returns the index of the found item, or nil if it doesnt exist, lua is not true bool so anything counts as true as long as its not nil
table.remove(inRange,table.find(InRange,Player))
RandomFunctionAfterTheyLeaveRange(Player) -- whatever function you need
end
The simple solution to your problem would be to use the pre-created method:
That being said, you have to be careful setting up the region, as Region3 essentially just creates a box between the two points regardless of the angle you want.
The documentation equally also runs you through their method, if you want to try to replicate it in your own code.
for _,area in pairs(game.Workspace.Area:GetChildren()) do
local region = Region3.new(area.Position - (area.Size/2), area.Position + (area.Size/2))
local whitelist = game.Workspace:FindPartsInRegion3(region, area, 1000)
wait()
for _, parts in pairs(whitelist) do
if parts:FindFirstAncestor(Player.Name) then
if Region3Debounce == false then
Region3Debounce = true
FoundInRegion3 = true
AttachCamera = false
local goal = {
CFrame = CFrame.new(game.Workspace.Cameras[area.Name].Position)
}
local info = TweenInfo.new(.5,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
local tween = TweenService:Create(Camera,info,goal)
tween:Play()
end
end