Trying to Make a Safezone Table

Hello!

I’ve been working on a Safezone module for fun, But im running into some issues.

i’ve tried to add a Table that holds the UserID’s of the players that enter a specified safezone, And it works, it’s just that it inserts multiple instances of the same UserId into the table.

There’s also the issue of this only working for one player.
as soon as a 2nd player enters the Safezone, the already existing player gets their Safezone taken away.

I’ve tried adding another entire table, but that just made the problem worse.

Im Entirely Out of Ideas.

I’ve provided some screenshots and videos of the issue here:

local assets = script.Parent.Assets

local Safezone = {}
Safezone.PlayersInZone = {}

function Safezone.check(self, player)
	if player then
		if table.find(self.PlayersInZone, player.UserId) then
			return true
		else
			return false
		end
	end
end

function Safezone.updateSafeZone(self)
	local safezoneRegion = Region3.new(workspace.Safezone.Position - workspace.Safezone.Size / 2, workspace.Safezone.Position + workspace.Safezone.Size / 2)
	local partsInSafezone = workspace:FindPartsInRegion3(safezoneRegion, nil)

	local playersInSafezone = {}

	for _, part in ipairs(partsInSafezone) do
		local character = part.Parent
		if character and character:IsA("Model") then
			local player = game:GetService("Players"):GetPlayerFromCharacter(character)

			if player and not self:check() then
				table.insert(playersInSafezone, player.UserId)
				if not character:FindFirstChild("ForceFieldHighlight") then
					local ffeffect = assets.ForceFieldHighlight:Clone() ffeffect.Parent = character
				end
				print(self.PlayersInZone)
			end
		end
	end

	for _, userId in ipairs(self.PlayersInZone) do
		if not table.find(playersInSafezone, userId) and not self:check() then
			local player = game:GetService("Players"):GetPlayerByUserId(userId)
			if player and player.Character:FindFirstChild("ForceFieldHighlight") then
				player.Character:FindFirstChild("ForceFieldHighlight"):Destroy()
				print(player.Name .. " has left the safezone.")
			end
		end
	end

	self.PlayersInZone = playersInSafezone
end

return Safezone

and this is how i run it on the server:

runService.Stepped:Connect(function()
	safezone:updateSafeZone()
end)

image

Any help is greatly appreciated! If you need any more information, don’t hesitate to ask!

2 Likes

To start out with, I’m not sure that having a table of the user ID’s is the best way to go about this. I’d consider logging the path of the rig’s humanoid root part in the table instead. It seems inefficient and hard to debug as you have the same User ID as the person you are testing this with.

From what I see, this should work if you test it out with a different player. The script might be getting confused on who is in the safezone and just only accounting for one person. From what I understand, I don’t think that the table can distinguish between the 16 different user ID’s as it does not save (or understand) where they came from.

1 Like

I’ve tested this in team test, with Player1 and Player2.
And what do you mean by “I don’t think that the table can distinguish between the different UserId’s”?
That’s one of the bugs, there should only be one for each player.

Or would a better way to go about this be CollectionService?

Does each player in team test have a different ID? I thought they all just had the same ID.

And what I meant by the thing you quoted was based on the assumption above. The data table can’t understand where the data came from (for example, a different player entering a safezone but has the same ID). It’s kind of hard for me to explain, but that’s the best I can do for now.

From what I understand, CollectionService would be an excellent option.

2 Likes

i’ll try using CollectionService then, Thanks for you help! :smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.