Region3 breaks when the same code gets copy pasted

Hello.

Please watch the video to get a good understanding.

https://youtu.be/dOWaKTEmClQ

safezone system: you enter, tools get parented somewhere else, you leave, you get tools back

In the first clip the code works fine as the 2nd safezone is excluded, in the 2nd clip the 2nd safezone is included and you can see that it completely breaks.
the code is pretty basic to understand, it’s a region 3 with some checks and when a player is in region3 we parent tools into a secure folder for storage so it’s a safezone, and when they leave they come back. you can see the game literally gets destroyed by using, the same code…

Read the commented code as real code, it’s commented out because when I test it like that, everything works (first clip), but when i unignore it, everything breaks. (second clip)

local region = Region3.new(workspace.Map.Safezones.Safezone1.Position - workspace.Map.Safezones.Safezone1.Size / 2, workspace.Map.Safezones.Safezone1.Position + workspace.Map.Safezones.Safezone1.Size / 2)
--local regionSecondSafezone = Region3.new(workspace.Map.Safezones.Safezone2.Position - workspace.Map.Safezones.Safezone2.Size / 2, workspace.Map.Safezones.Safezone2.Position + workspace.Map.Safezones.Safezone2.Size / 2)
local region2 = Region3.new(workspace.Map.TimeZones.x2.Position - workspace.Map.TimeZones.x2.Size / 2, workspace.Map.TimeZones.x2.Position + workspace.Map.TimeZones.x2.Size / 2)
local region3 = Region3.new(workspace.Map.TimeZones.x4.Position - workspace.Map.TimeZones.x4.Size / 2, workspace.Map.TimeZones.x4.Position + workspace.Map.TimeZones.x4.Size / 2)

local blacklistedNames = {
	"FirstPlace";
	"SecondPlace";
	"ThirdPlace";
}

local regionedPlayers = {}
--local safezone2Players = {} 
local x2regionedPlayers = {}
local x4regionedPlayers = {}

coroutine.wrap(function()
	task.wait(5)
	while true do
		local partsInRegion = workspace:FindPartsInRegion3(region, workspace.Map.Safezones.Safezone1, 10000)
		--local partsInSecondSafezone = workspace:FindPartsInRegion3(regionSecondSafezone, workspace.Map.Safezones.Safezone2, 10000)
		local partsInRegion2 = workspace:FindPartsInRegion3(region2, workspace.Map.TimeZones.x2, 10000)
		local partsInRegion3 = workspace:FindPartsInRegion3(region3, workspace.Map.TimeZones.x4, 10000)
		for _, v in ipairs(partsInRegion) do
			if v.Name == "HumanoidRootPart" then
				if v.Parent:FindFirstChild("Humanoid") then
					if not table.find(blacklistedNames, v.Parent.Name) then
						if v.Parent.HumanoidRootPart then
							local charPlayer = v.Parent
							local Player = Players:GetPlayerFromCharacter(charPlayer)
							if not table.find(regionedPlayers, Player.Name) then
								table.insert(regionedPlayers, Player.Name)
							end
							Player.Values.Safezone.Value = true
							require(ServerScriptService.Server.Modules.Main).CombatlogText(Player, "You are inside the safezone! If you leave, you will not lose 15% of your Time.", Color3.fromRGB(0, 255, 0))
							if ServerStorage.Storage.UFO.Flying.Value == false then
								General:FireClient(Player, "EnableUI")
							end
							Player.Character.Humanoid:UnequipTools()
							for a, b in ipairs(Player.Backpack:GetChildren()) do
								if b:IsA("Tool") then
									local tempTool = Instance.new("StringValue")
									tempTool.Name = "tempTool"
									tempTool.Value = b.Name
									tempTool.Parent = Player.Values.SafezoneTools
									b.Parent = ServerStorage.Storage.Resources.User[Player.Name].TempTools
								end
							end
							for c, d in ipairs(Player.Character:GetChildren()) do
								if d:IsA("Tool") then
									local tempTool = Instance.new("StringValue")
									tempTool.Name = "tempTool"
									tempTool.Value = d.Name
									tempTool.Parent = Player.Values.SafezoneTools
									d.Parent = ServerStorage.Storage.Resources.User[Player.Name].TempTools
								end
							end
						end
					end
				end
			end
		end
		--[[for _, v in ipairs(partsInSecondSafezone) do
			if v.Name == "HumanoidRootPart" then
				if v.Parent:FindFirstChild("Humanoid") then
					if not table.find(blacklistedNames, v.Parent.Name) then
						if v.Parent.HumanoidRootPart then
							local charPlayer = v.Parent
							local Player = Players:GetPlayerFromCharacter(charPlayer)
							if not table.find(regionedPlayers, Player.Name) then
								table.insert(regionedPlayers, Player.Name)
							end
							Player.Values.Safezone.Value = true
							require(ServerScriptService.Server.Modules.Main).CombatlogText(Player, "You are inside the safezone! If you leave, you will not lose 15% of your Time.", Color3.fromRGB(0, 255, 0))
							if ServerStorage.Storage.UFO.Flying.Value == false then
								General:FireClient(Player, "EnableUI")
							end
							Player.Character.Humanoid:UnequipTools()
							for a, b in ipairs(Player.Backpack:GetChildren()) do
								if b:IsA("Tool") then
									local tempTool = Instance.new("StringValue")
									tempTool.Name = "tempTool"
									tempTool.Value = b.Name
									tempTool.Parent = Player.Values.SafezoneTools
									b.Parent = ServerStorage.Storage.Resources.User[Player.Name].TempTools
								end
							end
							for c, d in ipairs(Player.Character:GetChildren()) do
								if d:IsA("Tool") then
									local tempTool = Instance.new("StringValue")
									tempTool.Name = "tempTool"
									tempTool.Value = d.Name
									tempTool.Parent = Player.Values.SafezoneTools
									d.Parent = ServerStorage.Storage.Resources.User[Player.Name].TempTools
								end
							end
						end
					end
				end
			end
		end]]
		for _, v in ipairs(partsInRegion2) do
			if v.Name == "HumanoidRootPart" then
				if v.Parent:FindFirstChild("Humanoid") then
					if not table.find(blacklistedNames, v.Parent.Name) then
						local charPlayer = v.Parent
						local Player = Players:GetPlayerFromCharacter(charPlayer)
						if not table.find(x2regionedPlayers, Player.Name) then
							table.insert(x2regionedPlayers, Player.Name)
						end
						Player.Values.x2TimeZone.Value = true
					end
				end
			end
		end
		for _, v in ipairs(partsInRegion3) do
			if v.Name == "HumanoidRootPart" then
				if v.Parent:FindFirstChild("Humanoid") then
					if not table.find(blacklistedNames, v.Parent.Name) then
						local charPlayer = v.Parent
						local Player = Players:GetPlayerFromCharacter(charPlayer)
						if not table.find(x4regionedPlayers, Player.Name) then
							table.insert(x4regionedPlayers, Player.Name)
						end
						Player.Values.x4TimeZone.Value = true
					end
				end
			end
		end
		for _, v in ipairs(Players:GetPlayers()) do
			if not table.find(regionedPlayers, v.Name) then
				v.Values.Safezone.Value = false
				require(ServerScriptService.Server.Modules.Main).CombatlogText(v, "You are outside of the safezone. If you leave, you will lose 15% of your Time for Combat Logging.", Color3.fromRGB(255, 0, 0))
				General:FireClient(v, "DisableUI")
				for a, b in ipairs(v.Values.SafezoneTools:GetChildren()) do
					if b:IsA("StringValue") then
						ServerStorage.Storage.Resources.User[v.Name].TempTools:FindFirstChild(b.Value):Clone().Parent = v.Backpack
						b:Destroy()
					end
				end
				for c, d in ipairs(ServerStorage.Storage.Resources.User[v.Name].TempTools:GetChildren()) do
					if d:IsA("Tool") then 
						d:Destroy()
					end
				end
			end
			--[[if not table.find(safezone2Players, v.Name) then
				v.Values.Safezone.Value = false
				print(tostring(v.Name).." | Values = "..tostring(v.Values.Safezone.Value))
				require(ServerScriptService.Server.Modules.Main).CombatlogText(v, "You are outside of the safezone. If you leave, you will lose 15% of your Time for Combat Logging.", Color3.fromRGB(255, 0, 0))
				General:FireClient(v, "DisableUI")
				for a, b in ipairs(v.Values.SafezoneTools:GetChildren()) do
					if b:IsA("StringValue") then
						ServerStorage.Storage.Resources.User[v.Name].TempTools:FindFirstChild(b.Value):Clone().Parent = v.Backpack
						b:Destroy()
					end
				end
				for c, d in ipairs(ServerStorage.Storage.Resources.User[v.Name].TempTools:GetChildren()) do
					if d:IsA("Tool") then 
						d:Destroy()
					end
				end
			end]]
			if not table.find(x2regionedPlayers, v.Name) then
				v.Values.x2TimeZone.Value = false
			end
			if not table.find(x4regionedPlayers, v.Name) then
				v.Values.x4TimeZone.Value = false
			end
		end
		regionedPlayers = {}
		--safezone2Players = {}
		x2regionedPlayers = {}
		x4regionedPlayers = {}
		task.wait(0.01)
	end
end)()

Thanks! This has been on my mind for couple of days now, and no succes in any solution.

1 Like

I would recommend using GetPartsInPart or GetPartBoundsInBox instead of Region3. Easier to work with and setup.

Someone told me on HiddenDevs, yup. Thanks.

But that wouldn’t answer my question on why it’s breaking. Why would it work with 1 safezone, but not with 2? It’s literally the same code.

Too much code for me to read right now, but instead of copy/pasting code, you should have one script handle all safe zones.

Alright.

How would I make 1 table containing a check for all Region3 zones? There are 4 zones in teh game which all do a seperate thing.

Actually, it might be better to have this script inside the player’s character instead and use a touch event.

I’ve tried that but then eventually the tools will be given to you when you are in the safezone. I went from Touch and TouchEnded to Region3 and now I have to do some GetPartsInParts

Touch will work for this, but TouchEnded is unreliable.

Touched and TouchEnded are fine, you should just make sure that each respond to the same part of a character’s rig, typically the ‘HumanoidRootPart’.

1 Like

Maybe I was doing it wrong before, testing it now it seems to work perfectly. Thanks for the update

FindPartsInRegion3 is additionally deprecated as well.


I recommend this solution, since they do work fine. Touched and TouchEnded are always reliable on the server, as long as the server has a reasonable framerate, and the client isn’t going too fast. However, sometimes the touched event will cause slowness if fired too much, so a debounce must also be implemented (even with the debounce a slight amount of lag can be noticed if fired over about 1,000 times per second).

The server doesn’t have a framerate, you’re likely referring to its physics-step rate. I agree with the debounce statement, especially if your game has lots of moving parts.

Really late to this as I was away, but one warning is that the touch event can be triggered by the client from any location by an exploiter (both my teleporting or remotely firing a touch event). I would recommend also doing a server-side position check to verify if the touch is valid. However, if the exploiter has teleported, that may prove difficult without extra systems detecting illegal movement.

I have made a module for this main reason being to not have to copying the same functions into multiple scripts for creating regions. Here is the model to region module: MyRegion3ModuleNew - Roblox . This uses the all types of touching methods such as the new Overlap Parameters .

I have tried TouchEnded and touched and checking for a HumanoidRootPart, but eventually if you would spam at the edge of the TouchEnded and Touched part, you’d get the tools in the safezone. That should not be allowed, hence the reason I got redirected to Region3.

Well, now you know, that you should be using the ZoneModule in that case. (I think it’s called ZonePlus or something like that, but I can’t remember)
Anyways, I think that is the best solution for this, unless constantly checking if the character is in the area.

1 Like