Safezones with collision groups

local ServerStorage = game:GetService("ServerStorage")
local PhysicsService = game:GetService("PhysicsService")
local CombatTaggedFolder = ServerStorage.CombatTaggedPeople
local Zone = script.Parent
local Zones = "Zones"
local nontaggedplayers = "taggedplayers"

PhysicsService:CreateCollisionGroup(Zones)
PhysicsService:CreateCollisionGroup(nontaggedplayers)

PhysicsService:SetPartCollisionGroup(Zone, Zones)

Zone.Touched:Connect(function(touched)
	if touched.Parent:FindFirstChild("Humanoid") then	
		if not CombatTaggedFolder:FindFirstChild(touched.Parent.Name) then
			print("player isnt tagged")
			PhysicsService:SetPartCollisionGroup(touched.Parent.UpperTorso, nontaggedplayers)
		else
			print("player is tagged")
		end
	end
end)

PhysicsService:CollisionGroupSetCollidable(nontaggedplayers, Zones, false)

the prints work but when im not tagged i still cant enter the zone

You’ll have to change the collision groups of all parts of the player’s character to nontaggedplayers instead of just the UpperTorso.

what about just da humanoidrootpart will that work to

Probably not, unless all other parts of the character have CanCollide off. Here’s an article that accomplishes something similar—take a look at Code Sample 4.


Edit: Forgot to link the article, sorry. :man_facepalming:

Try this code, let me know if it works.

local ServerStorage = game:GetService("ServerStorage")
local PhysicsService = game:GetService("PhysicsService")
local CombatTaggedFolder = ServerStorage.CombatTaggedPeople
local Zone = script.Parent
local Zones = "Zones"
local nontaggedplayers = "taggedplayers"

PhysicsService:CreateCollisionGroup(Zones)
PhysicsService:CreateCollisionGroup(nontaggedplayers)

PhysicsService:SetPartCollisionGroup(Zone, Zones)

local function setCollisionGroup(character, groupName)
	for _, child in ipairs(character:GetChildren()) do
		if child:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(child, groupName)
		end
	end
	character.DescendantAdded:Connect(function(descendant)
		if descendant:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(descendant, groupName)
		end
	end)
end

Zone.Touched:Connect(function(touched)
	if touched.Parent:FindFirstChild("Humanoid") then	
		if not CombatTaggedFolder:FindFirstChild(touched.Parent.Name) then
			print("player isnt tagged")
			setCollisionGroup(touched.Parent, nontaggedplayers)
		else
			print("player is tagged")
		end
	end
end)

PhysicsService:CollisionGroupSetCollidable(nontaggedplayers, Zones, false)
local ServerStorage = game:GetService("ServerStorage")
local PhysicsService = game:GetService("PhysicsService")
local CombatTaggedFolder = ServerStorage.CombatTaggedPeople
local Zone = script.Parent
local Zones = "Zones"
local nontaggedplayers = "nontaggedplayers"
local taggedplayers = "taggedplayers"

PhysicsService:CreateCollisionGroup(Zones)
PhysicsService:CreateCollisionGroup(nontaggedplayers)
PhysicsService:CreateCollisionGroup(taggedplayers)

PhysicsService:SetPartCollisionGroup(Zone, Zones)

Zone.Touched:Connect(function(touched)
	if touched.Parent:FindFirstChild("Humanoid") then	
		if not CombatTaggedFolder:FindFirstChild(touched.Parent.Name) then
			for i,v in pairs(touched.Parent:GetChildren()) do
				if v:IsA("Part") or v:IsA("MeshPart") then
					PhysicsService:SetPartCollisionGroup(v, nontaggedplayers)
				end
			end
		end
	end
end)

Zone.TouchEnded:Connect(function(touched)
	if touched.Parent:FindFirstChild("Humanoid") then	
		for i,v in pairs(touched.Parent:GetChildren()) do
			if v:IsA("Part") or v:IsA("MeshPart") then
				PhysicsService:SetPartCollisionGroup(v, taggedplayers)
			end
		end
	end
end)

PhysicsService:CollisionGroupSetCollidable(nontaggedplayers, Zones, false)
PhysicsService:CollisionGroupSetCollidable(taggedplayers, Zones, true)

thanks for da help my safezones work now :smiley:

1 Like