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

How do I check if a player is not in the region anymore without using "else" because it won't work?

local region = Region3.new(Vector3.new(0,0,0), Vector3.new(182.25, 67, 101.75))

local player = game:GetService("Players")

local part = Instance.new("Part")
part.Anchored = true
part.Size = region.Size
part.Position = part.Position + Vector3.new(8.375, 24.25, 42.875)
part.Parent = game.Workspace
part.CanCollide = false
part.Transparency = 0.5

while true do
	wait()
	local partsInRegion = workspace:FindPartsInRegion3(region, part, 1000)
	for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			print("Player is inside")
		end
	end
end

1 answer

✓ Accepted answer

I would make a table storing players that got in the Region, and when you don't find those players anymore, it means that they left

local region = Region3.new(Vector3.new(0,0,0), Vector3.new(182.25, 67, 101.75))

local player = game:GetService("Players")

local PlayersTable = {}
local TempPlayers = {}

local part = Instance.new("Part")
part.Anchored = true
part.Size = region.Size
part.Position = part.Position + Vector3.new(8.375, 24.25, 42.875)
part.Parent = game.Workspace
part.CanCollide = false
part.Transparency = 0.5

while wait() do
	local partsInRegion = workspace:GetPartsInPart(part)
	for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") and not table.find(TempPlayers, part.Parent) then
			if not table.find(PlayersTable, part.Parent) then
				print(part.Parent.Name .. " got inside")
				table.insert(PlayersTable, part.Parent)
			end
			table.insert(TempPlayers, part.Parent) -- no matter if it's new or old, you add to this table
		end
	end
	-- to check if someone left you go over Players table and check if there are players that are no longer in TempPlayers
	for i, plr in pairs(PlayersTable) do
		if not table.find(TempPlayers, plr) then
			print(plr.Name .. " left")
			table.remove(PlayersTable, i) -- remove this player from players table because it's no longer inside the region
		end
	end
	table.clear(TempPlayers) -- clear so you can check again
end

try this script, I didn't test it but it should theoretically work, if something is wrong tell me and i'll try to fix it

3 comments

0

Hmm it doesn't work. It is not printing anything but atleast it doesn't give any errors. You know how to fix this?

0

looks like FindPartsInRegion3 is deprecated, use GetPartsInPart instead. i edited the script

0

omg it works.. thank you!!

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