How would i get players that entered a zone / left a zone

Trying to set up some sort of safe zone and got here

local SafeZone = script.Parent
local Zone = SafeZone.Zone
local RunService = game:GetService("RunService")
local ThingsInSafeZone
local Connection
local StuffInSafeZone

local function MakePlayerSafe(MaybePlayer)
	if MaybePlayer.Parent:FindFirstChild("IsInSafeZone") then
		
	end
end

RunService.Heartbeat:Connect(function(DT)
	Connection = Zone.Touched:Connect(function() end)
	ThingsInSafeZone = Zone:GetTouchingParts()
	for i, v in ipairs(ThingsInSafeZone) do
		MakePlayerSafe(v)
	end
	
	Connection:Disconnect()
end)

And then now im not sure how i would check if the player has left the zone
(im setting a value to true if the player is in the zone)

2 Likes
local part = game.Workspace.Part

part.Touched:Connect(function()
       print("player has entered the zone")
end)

part.TouchEnded:Connect(function()
      print("player has left the zone")
end)
1 Like

I think you should try the following :

  1. make a script inside of the zone
  2. write the following code inside of it
local zone = script.Parent
local playersInside = {}

local function getPlayersInside()
   for i, player in pairs(game:GetService("Players"):GetPlayers()) do
      local character = player.Character
      local rootPart = character.HumanoidRootPart

      local maxDistance = (zone.Size.X + zone.Size.Z) / 3
      if (rootPart.Position - zone.Position).Magnitude <= maxDistance then
         table.insert(playersInside, player)
      elseif table.find(playersInside, player) and (rootPart.Position - zone.Position).Magnitude > maxDistance then
         table.remove(playersInside, player)
      end
   end
end

while wait() do
    getPlayersInside()
    for i, player in pairs(playersInside) do
       -- do something
    end
end
1 Like

just use this module its perfect and easy to use lol

1 Like
local Enumeration = Enum
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local RunService = Game:GetService("RunService")
local Part = Workspace.Part --Safezone part.

local _OverlapParams = OverlapParams.new()
_OverlapParams.FilterType = Enumeration.RaycastFilterType.Blacklist

local PlayersTable = {}

local function OnHeartbeat()
	local Parts = Workspace:GetPartsInPart(Part, _OverlapParams)
	for _, Part in ipairs(Parts) do
		local Model = Part:FindFirstAncestorOfClass("Model")
		if not Model then continue end
		local ForceField = Model:FindFirstChildOfClass("ForceField")
		if ForceField then continue end
		local Humanoid = Model:FindFirstChildOfClass("Humanoid")
		if (not Humanoid) or Humanoid.Health <= 0 then continue end
		local Player = Players:GetPlayerFromCharacter(Model)
		if not Player then continue end
		PlayersTable[Player] = true
		ForceField = Instance.new("ForceField")
		ForceField.Parent = Model
	end
	
	for Player, _ in pairs(PlayersTable) do
		local Character = Player.Character
		if not Character then PlayersTable[Player] = nil continue end
		local ForceField = Character:FindFirstChildOfClass("ForceField")
		if not ForceField then PlayersTable[Player] = nil continue end
		local Descendants = Character:GetDescendants()
		local Flag
		for Index, Descendant in ipairs(Descendants) do
			if not (Descendant:IsA("BasePart")) then continue end
			if table.find(Parts, Descendant) then Flag = true break end
		end
		if Flag then continue end
		ForceField:Destroy()
		PlayersTable[Player] = nil
	end
end

local function OnPlayerRemoving(Player)
	PlayersTable[Player] = nil
end

RunService.Heartbeat:Connect(OnHeartbeat)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

This is working for me, just requires a single part placed inside the ‘Workspace’ container named ‘Part’.

1 Like

Tried this with my Zone and it wouldn’t work great, only would detect player if they a pretty good amount into the zone

tried setting something up with this

Zone.Touched:Connect(function(Touched)
	local Limb = Touched
	local MaybeCharacter = Touched.Parent
	
	if not MaybeCharacter:FindFirstChild("Humanoid") then return end
	if table.find(StuffInSafeZone, MaybeCharacter) then return end
	
	local Character = MaybeCharacter
	
	table.insert(StuffInSafeZone, Character)
	print(Character.Name .. " Added")
end)

Zone.TouchEnded:Connect(function(Touched)
	local Limb = Touched
	local MaybeCharacter = Touched.Parent

	if not MaybeCharacter:FindFirstChild("Humanoid") then return end
	if not table.find(StuffInSafeZone, MaybeCharacter) then return end

	local Character = MaybeCharacter

	table.remove(StuffInSafeZone, table.find(StuffInSafeZone, Character))
	print(Character.Name .. " Removed")
end)

and just walking through the safezone would spam output with player added and player removed

i would, but i like “making my own stuff”, makes it feel more unique to me

And this

It works, although i do not understand certain bits of the code