Zone+ v1 (deprecated) | Retrieving players within an area/zone

Will this work for jumpscares?

This helped a lot, thank you!
(30 chars)

1 Like

I really love Zone +, but for some reason it isn’t working for me:

local RP = game:GetService("ReplicatedStorage")
local ZP = require(4664437268)
local ZS = require(ZP.ZoneService)
local zonePart = game.Workspace.ZonePart
local zone = ZS:createZone("Zone", zonePart)

--------------------------------------------------------

local playersInZone = zone:getPlayers()

zone.playerAdded:Connect(function(plr)
	
	print(plr.Name.." Joined!")
	
end)

zone.playerRemoving:Connect(function(plr)
	
	print(plr.Name.." Left!")
	
end)

zone:initLoop()

Nothing will print, and I won’t get any errors (ZonePart is a Part with CanCollide off)

I think the zonePart variable has to be a folder or model. Either one should work.

2 Likes

Thank you so much!!! I spent hours trying to find my mistake and just couldn’t, you’re a life saver!

1 Like

Hi, what are the best practices for setting up a zone / the limitations of this?

Currently I had just set up multiple zones, building on from the first one I had implemented as a test.
Here’s what they look like in studio (color-coded)

Having all these zones enabled with a basic playerAdded and initLoop shoots my server Script Activity usage up to 25-30%. (I’m assuming this from the MicroProfiler as well as disabling/enabling)

The same amount of zones works fine on the client without any significant frame drops.

Is the problem the number of parts the zones are over? Should I create flat zones under the map and use the height parameter? I’m wondering if I can find a solution to this, for now I’ve temporarily disabled them on the server. Any help is appreciated :slightly_smiling_face:

Good question, I’ve included a Best Practises section to cover all your points and some additional:

In short, for your situation, use Zone+ on the client, minimise the height (therefore volume) of your zones where possible, and specify a longer delay for the loop, such as :initClientLoop(3).

If a collection of zone parts have a height of 10, while another has a height of 9 and an additionalHeight of 1, then they both effectively have the same height (and volume). Reducing any zones total height though will improve its performance (this is negligible for small zones).

3 Likes

Hello, Great Zone+
If you keep the zone area 200x200x200 or less, on average how many zones can you have and not see performance issues?

10, 20, 50 , 100?

Thanks!

This depends on a few factors you’ll need to determine yourself:

  • Whether the zone is on the server or client (and calling initClientLoop)
    If the latter, a zones volume can be as large as you like with minimal-no impact on performance as it only performs raycast checks. Otherwise if server, then you will notice performance impacts the greater a zones volume due to additional Region3 checks.

  • How often checks are performed
    You could construct 1000 zones of size 1000x1000x1000 and be perfectly fine. It’s when you begin to call :getPlayer() and :getPlayers() frequently (which initLoop() does 600 times per minute per zone unless you specify a greater delay such as initLoop(3)) for lots of zones where things become extremely intensive. For instance, if you have 100 zones of size 200x200x200 and call initLoop() on all of these without specifying a delay, you’ll be performing 60,000 Region3 and Raycast checks over a volume of 800,000,000 studs cubed per minute. That’s going to hurt.

On the server, If you intend to setup lots of zones on the server with large volumes, or if your zones cover your entire map, you’ll honestly be better off setting up your own detection system which fires a raycast from every players HRP every interval.

Zone+ events on the server are primarily optimised and intended for servers with lots of players with zones that do not cover a large proportion of the map.

4 Likes

Just wanted to point out that the current code doesn’t seem to support wedges or parts at odd angles. Only partial workaround this is by unioning said parts.

1 Like

I’m having an issue where it only makes a zone for one part in the model I am making a zone out of. This is my code:

local serverStorage = game:GetService("ServerStorage")

local zonePlus = require(4664437268)
local zoneService = require(zonePlus.ZoneService)

local zoneParts = workspace:WaitForChild("ZoneParts")
local zone = zoneService:createZone("ArenaZone", zoneParts)

local items = serverStorage:WaitForChild("Items")

zone.playerAdded:Connect(function(player)
	if player.Character then
		items:FindFirstChild(serverStorage.PlayerData[player.Name].EquippedItem.Value):Clone().Parent = player.Backpack
	end
end)

zone.playerRemoving:Connect(function(player)
	if player.Character then
		player.Character:FindFirstChildOfClass("Humanoid"):UnequipTools()
		
		local item = player.Backpack:FindFirstChild(serverStorage.PlayerData[player.Name].EquippedItem.Value)

		if item then
			item:Destroy()
		end
	end
end)

zone:initLoop()
1 Like

Your code appears to be fine at a glance. Can you send a picture of zoneParts and their descendants in workspace? Have you made sure the parent is a model or folder?

3 Likes

Sure.
zoneparts

1 Like

I’m making a room detection system for my game and it’s worked great so far to detect which room each player is in.
I know this isn’t a feature of the module, and neither is it intended, but what do you suggest for detecting non-player objects such as a thermometer detecting which room it is currently in.

There’s a few methods you could use in your scenario and it ultimately depends on how your tools/objects behave (i.e. can they move at all, can they be picked up by players, etc).

First idea which springs to mind: group invisible parts together to represent a room and place them slightly below the room itself, then whenever you need to know which room the thermometer is in, fire a raycast downwards and if it hits one of these parts you can deduce which room its in.

2 Likes

Has anyone used this for hitboxes? All the existing solutions I have looked at aren’t good for my use case so far. using just regular .Touched doesnt reliably fire if the hitbox isnt moving.

I was wondering this aswell while looking at this topic.

Zone+ can be used to generate hitboxes. Keep in mind it’s primarily intended to work with static areas as apposed to dynamically changing areas. For this reason I’d explore other resources such as swordphin’s Raycast Hitbox module:

3 Likes

Hey :wave: I’ve been using Zone+ recently (it’s an amazing module). However, I’ve been running into some problems recently.

I’ve been making two zones for a music changer and an environment name popup thingy. There has been this bug and it’s IS SO FRUSTRATING. This is happening on the client (I use it correctly):

This is a class that is integrated with Quenty’s Binder class

function EnvironmentRegion:_connectModel(model)
	local zone = Zone.new(model)
	
	zone.playerAdded:Connect(function(player)
		print(player.Name, "has entered", self:GetEnvironmentName())
		EnvironmentName:Set("DisplayedText", self:GetEnvironmentName())
	end)
	
	zone.playerRemoving:Connect(function(player)
		print(player.Name, "has left", self:GetEnvironmentName())
	end)
	
	self._maid:GiveTask(zone)
	zone:initClientLoop(0.01)

	print("Created EnvironmentRegion Zone For Model:", model:GetFullName())
end
-- I know this says MusicChangePart but it is actually a model (i'm looking to
-- change the name later)
function MusicChangePart:_connectModel(model)
	local zone = Zone.new(model)

	zone.playerAdded:Connect(function(player)
		print("Switch background music")
		SwitchBackgroundMusic({
			SoundId = self:GetAudioAssetId(),
		})
	end)
	
	zone.playerRemoving:Connect(function(player)
		print("Left background")
		SwitchBackgroundMusic({
			SoundId = SwitchBackgroundMusic.DefaultBackgroundMusic,
		})
	end)
	
	self._maid:GiveTask(zone)
	print("Created MusicChangePart Zone For Model:", model:GetFullName())
	zone:initClientLoop(0.01)
end

The major problem is that the behavior is extremely inconsistent. Sometimes, one zone works. Sometimes the other. Sometimes both zones work, and sometimes none work. It is really frustrating.

Also, It prints that the zones are created (both zones). I think it’s something to do with playerAdded and playerRemoving.

No errors at all.

Thanks.

Will this worked with lightings and atmospheres on only player client that won’t affect other players client?