How to find if the player is in the safe zone?

So I don’t know where to start with my safe zone part of my script.

I want to use

BasePart:GetTouchingParts()

But I don’t think it’ll work since the safe zone part is CanCollide false.

How should I do this?

You should use Region3. EgoMoose made a module for it.

2 Likes

Can you give more explanation to it? Like what function I should use?

Here’s an example peice of code:

local module = require(game.ReplicatedStorage.RotatedRegion3)
local inputRegion = module.fromPart(game.Workspace.SafeZone)
local region3Parts = inputRegion:cast({},1000000) --just put a giant number so there can be alot of parts in the table

for i,v in pairs(region3Parts) do
local player = game.Players:GetPlayerFromCharacter(v.Parent)

if player then
--do stuff
end
end

2 Likes

Just add

YourPart.Touched:Connect(function() end)

To the top of your code. Now there is a TouchInterest in your part and can detect parts even if its CanCollide is set to false

Ok its not working right now:

local module = require(game.ReplicatedStorage.Modules.RotatedRegion3)
local inputRegion = module.fromPart(game.Workspace.Safe)
local region3parts = inputRegion:cast({}, 1000000)

for i, v in pairs(region3parts) do
	local player = game.Players:GetPlayerFromCharacter(v.Parent)
	
	if player then
		player:WaitForChild("IsSafe").Value = true
	end
end

local function compare(old,new)
local playersInOld = {}
local playersInNew = {}

for i,v in pairs(old) do

	local player = game.Players:GetPlayerFromCharacter(v.Parent)
	
	if player then
		playersInOld[player] = true
	end

end

for i,v in pairs(new) do
local player = game.Players:GetPlayerFromCharacter(v.Parent)

	if player then
		playersInNew[player] = true
	end

end

for i,v in pairs(old) do

local inNew = playersInNew[v.Parent]

if inNew == nil then
	v.IsSafe.Value = false
	else
	v.IsSafe.Value = true	
end

end
end

local oldTable = nil
local module = require(game.ReplicatedStorage.Modules.RotatedRegion3)
while wait() do
if oldTable == nil then
local inputRegion = module.fromPart(game.Workspace.Safe)
local region3parts = inputRegion:cast({}, 1000000)

for i, v in pairs(region3parts) do
local player = game.Players:GetPlayerFromCharacter(v.Parent)

if player then
	player:WaitForChild("IsSafe").Value = true
end

end
oldTable = region3parts
else
local inputRegion = module.fromPart(game.Workspace.Safe)
local region3parts = inputRegion:cast({}, 1000000)
compare(oldTable,region3parts)
end
end

Um you didn’t explain anything and just put a script down.

Also I get this error:

IsSafe is not a valid member of part

So there’s something wrong with your code.

What line of code is it? I’ll explain once I get it to work.

		v.IsSafe.Value = false
local function removePlayersNotIn(parts)
	
	local players = {}
	
	
	for i,v in pairs(parts) do
		local player = game.Players:GetPlayerFromCharacter(v.Parent)
		
		if player then
			players[player] =true
		end
	end
	
	for i,v in pairs(game.Players:GetPlayers()) do
		local intable = players[v]
		
		if intable == nil then
			if v.IsSafe.Value == true then
				v.IsSafe.Value = false
			end
		end
	end
	
end
local module = require(game.ReplicatedStorage.RotatedRegion3)
while wait() do

local inputRegion = module.fromPart(game.Workspace.Safe)
local region3parts = inputRegion:cast({}, 1000000)

for i, v in pairs(region3parts) do
	local player = game.Players:GetPlayerFromCharacter(v.Parent)
	
	if player then
		player:WaitForChild("IsSafe").Value = true
	end
end

removePlayersNotIn(region3parts)

end

In the beginning, we have a function, that sets all players outside of the safe zone’s IsSafe value to false. We pass one argument, the region parts table. I then set up a dictionary to get the players in the parts table. It does a for i,v in pairs loop to index through every part in the table, and if it finds a player, it sets their value in the table to true. If the player isn’t in the table, it returns nil when looking for their value in the table. It then loops through all of the players, and if the table returns nil for them, and their InSafe value is true, it sets it to false. Now, we have a while loop to repeat code. It creates a Region3 from the part, and then gets a table of all of the parts, with nothing to ignore, and a part limit I think will never be reached. It then loops through all the stuff in the table and if it finds a players character, it set’s their InSafe value to true. It then calls the removePlayersNotIn() function, with the region3parts as an argument. It then does what I explained the function at the beginning does.

1 Like

Please search before posting threads.
https://devforum.roblox.com/search?q=safe%20zone%20category%3A55

There’s a slight problem. Every time you check it the game freezes for a bit.

AT this part:

while wait() do

instead I did

while wait(1) do

But it is still freezing.

EDIT: I also tried putting a small number for the module.fromPart() function and it stopped freezing, but then the IsSafe thing stopped working.

I don’t know what’s wrong, but as an alternative, you can try this. It isn’t as reliable as Region3, but if it has problems use this.

local part = game.Workspace.Safe

part.Touched:Connect(function(hit)

local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if plr then
plr.IsSafe.Value = true
end
end)

part.TouchEnded:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if plr then
plr.IsSafe.Value = false
end
end)