I'm using Region3 for this this door that I'm creating. The door opens perfectly fine when the player is inside the region. When it comes to closing it, I'm having a lot of trouble. How would I go about seeing if there are no players in the region?
Here's a general gist of the code opening the door
local region = Region3.new(pos1, pos2) while true do wait(1) local partsInRegion = game.Workspace:FindPartsInRegion3(region, nil, math.huge) local playersFound = {} for _, part in pairs(partsInRegion) do if part.Parent:FindFirstChild("Humanoid") ~= nil then print("PLAYER!") playersFound[part.Parent.Name] = part.Parent end end for plrName, char in pairs(playersFound) do if char then if opened == false then Door1Open:Play() wait(openWait) opened = true end end end end
I think it would be easier if Player:DistanceFromCharacter()
was used.
I would go about it like this
local function GetPlayersInDistance(vec, dist) local players = {} for name, player in pairs(game:GetService("Players"):GetPlayers()) do if player.Character and typeof(vec) == "Vector3" and type(dist) == "number" then local dis = player:DistanceFromCharacter(vec) if dis <= dist and dis > 0 then table.insert(players, player) end end end return players or {} end -- Example for returning a table of all players, 50 distance from door.Position (vector3) local distance = 50 local nearby = GetPlayersInDistance(door.Position, distance)