Team-only Door not working

I’m trying to get this Team only Door, ignore the fact it’s called GroupDoor
to work, but it doesn’t seem to be working.

I’m getting this error in output

21:00:44.508 GroupDoor is not a valid member of PhysicsService “PhysicsService” - Server - Script:8


local PhysicsService = game:GetService("PhysicsService")
local team = "Grand Chancellor"
local doorcollisiongroup = "GroupDoor"

PhysicsService:RegisterCollisionGroup(doorcollisiongroup)
PhysicsService:CollisionGroupSetCollidable(doorcollisiongroup, doorcollisiongroup, false)
PhysicsService.GroupDoor.CollisionGroup(script.Parent, doorcollisiongroup)

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Player.Changed:Connect(function()
		if Player.Team == game.Teams[team] then
			for _,v in pairs(Character:GetDescendants()) do
				if v:IsA("BasePart") then
						PhysicsService.GroupDoor.CollisionGroup(v, doorcollisiongroup)
						PhysicsService:CollisionGroupSetCollidable(doorcollisiongroup, doorcollisiongroup, false)
						Player.Character.Humanoid.Changed:Connect(function()
							if Player.Team ~= game.Teams[team] then
								PhysicsService.GroupDoor.CollisionGroup(v, doorcollisiongroup)
								PhysicsService:RegisterCollisionGroup(doorcollisiongroup, doorcollisiongroup, true)
							end
							end)
			end
		end
	end
	end)
	end)
	end)

Any help will be greatly appreciated

The error you are encountering is due to the incorrect usage of the PhysicsService. The PhysicsService does not have a GroupDoor property or method. Instead, you should use the SetPartCollisionGroup method to set the collision group for parts.

PhysicsService:SetPartCollisionGroup(script.Parent, doorcollisiongroup)

I tried that before, But it said that SetpartCollisionGroup Was depreciated?

Can you try this? and let me know if It works.

local PhysicsService = game:GetService("PhysicsService")
local teamName = "Grand Chancellor"
local doorCollisionGroup = "GroupDoor"

-- Register the collision group if not already registered
if not PhysicsService:CollisionGroupExists(doorCollisionGroup) then
    PhysicsService:RegisterCollisionGroup(doorCollisionGroup)
end

-- Set collidable settings for the group
PhysicsService:CollisionGroupSetCollidable(doorCollisionGroup, doorCollisionGroup, false)

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        player.Changed:Connect(function()
            if player.Team == game.Teams[teamName] then
                for _, part in ipairs(character:GetDescendants()) do
                    if part:IsA("BasePart") then
                        part.CollisionGroupId = doorCollisionGroup
                        part.CanCollide = false
                        player.Character.Humanoid.Changed:Connect(function()
                            if player.Team ~= game.Teams[teamName] then
                                part.CollisionGroupId = 0  -- Set to default collision group
                                part.CanCollide = true
                            end
                        end)
                    end
                end
            end
        end)
    end)
end)

PS: Edited the code above and removed deprecated functions.

what makes you think a collision group name becomes a property of physicservice? Like I’m trying to figure out how you got to the conclusion that that was the case and that it must have a method CollisionGroup()?!

It seems you want to set the collision group of parts, you use the part’s collisiongroup property.
It could not be more clear:

Part.CollisionGroup = "My Group"

so replace all lines with this cryptic structure:

PhysicsService.GroupDoor.CollisionGroup(some instance, doorcollisiongroup)

to:

some instance.CollisionGroup = doorcollisiongroup

also in the loop you are constantly setting the collisiongroup collisions to true or false based on a team.
You know you can just do:

local physicsService = game:GetService("PhysicsService")
physicsService:RegisterCollisionGroup("team1Doors")
physicsService:RegisterCollisonGroup("team2Doors")
physicsService:RegisterCollisionGroup("Team1")
physicsService:RegisterCollisionGroup("Team2")

physicsService:CollisionGroupSetCollidable("team1Doors", "Team1", false)
physicsService:CollisionGroupSetCollidable("team2Doors", "Team2", false)
physicsService:CollisionGroupSetCollidable("Team1", "Team2", false)

--now you assign for each door the corresponding collisiongroup.

alternatively If you have more than two teams:

local collectionService = game:GetService("CollectionService")

local function addDoor(door: Instance)
   door.Touched:Connect(function(hit)
        local char = hit:GetAncestorOfClass("Model")
        if not char then return end

        local plr = game.Players:GetPlayerFromCharacter(char)
        if not plr then return end
---give the door a team attribute
        if plr.Team ~= door:GetAttribute("Team") then return end
        door.CanCollide = false
        
        task.wait(5)
        door.CanCollide = true
   end)
end

collectionService:GetInstanceAddedSignal("GroupDoor"):Connect(addDoor)
for _, d in ipairs(collectionService:GetTagged("GroupDoor")) do addDoor(d) end

and boom, now the door will set cancollide to false when a player of the corresponding team touches it.

Hope this helps!

1 Like

uhh, it is registercollision group, just so you know.
createcollision group is deprecated,
same with setcollisiongroup use BasePart.CollisionGroup instead.

The new version doesn’t work, This is the error given

21:45:37.579 CollisionGroupExists is not a valid member of PhysicsService “PhysicsService” - Server - Teamdoor:6

I’ve made some more changes and now it’s working properly, I’ve tested it.

local PhysicsService = game:GetService("PhysicsService")
local teamName = "Grand Chancellor"
local doorCollisionGroup = "GroupDoor"

-- Function to check if a collision group exists
local function collisionGroupExists(groupName)
	local success, _ = pcall(function()
		PhysicsService:GetCollisionGroupName(groupName)
	end)
	return success
end

-- Check if the collision group exists, if not, register it
if not collisionGroupExists(doorCollisionGroup) then
	PhysicsService:CreateCollisionGroup(doorCollisionGroup)
end

-- Set collidable settings for the group
PhysicsService:SetPartCollisionGroup(script.Parent, doorCollisionGroup)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		player.Changed:Connect(function(property)
			if property == "Team" then
				local team = player.Team
				if team and team.Name == teamName then
					for _, part in ipairs(character:GetDescendants()) do
						if part:IsA("BasePart") then
							PhysicsService:SetPartCollisionGroup(part, doorCollisionGroup)
							part.CanCollide = false
						end
					end
				else
					for _, part in ipairs(character:GetDescendants()) do
						if part:IsA("BasePart") then
							PhysicsService:SetPartCollisionGroup(part, 0)  -- Set to default collision group
							part.CanCollide = true
						end
					end
				end
			end
		end)
	end)
end)

hmmm, It seems to still not work for me,

Have you ensured that you’ve included the path to your door parts on line 19?

PhysicsService:SetPartCollisionGroup(script.Parent, doorCollisionGroup)

script.Parent refers to your Door Part.

Yep, I’ve done that, Still not working but no errors now other than the deprecated ones

You must have done something wrong, as it’s working properly on my side.

RobloxStudioBeta_re8PpCopgf

local PhysicsService = game:GetService("PhysicsService")
local teamName = "Grand Chancellor"
local doorCollisionGroup = "GroupDoor"

-- Function to check if a collision group exists
local function collisionGroupExists(groupName)
	local success, _ = pcall(function()
		PhysicsService:GetCollisionGroupName(groupName)
	end)
	return success
end

-- Check if the collision group exists, if not, register it
if not collisionGroupExists(doorCollisionGroup) then
	PhysicsService:CreateCollisionGroup(doorCollisionGroup)
end

-- Set collidable settings for the group
PhysicsService:SetPartCollisionGroup(workspace.GroupDoor, doorCollisionGroup)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		player.Changed:Connect(function(property)
			if property == "Team" then
				local team = player.Team
				if team and team.Name == teamName then
					for _, part in ipairs(character:GetDescendants()) do
						if part:IsA("BasePart") then
							PhysicsService:SetPartCollisionGroup(part, doorCollisionGroup)
							part.CanCollide = false
						end
					end
				else
					for _, part in ipairs(character:GetDescendants()) do
						if part:IsA("BasePart") then
							PhysicsService:SetPartCollisionGroup(part, 0)  -- Set to default collision group
							part.CanCollide = true
						end
					end
				end
			end
		end)
	end)
end)

I can’t see what the issue would be?

This script currently only reacts to team changes after a player joins the game. To ensure it also checks the player’s team upon joining, adjustments need to be made.

Here is the fixed one with many adjustments made, also I’ve added some prints for debug.

local PhysicsService = game:GetService("PhysicsService")
local teamName = "Grand Chancellor"
local doorCollisionGroup = "GroupDoor"

local function collisionGroupExists(groupName)
	local success, _ = pcall(function()
		PhysicsService:GetCollisionGroupName(groupName)
	end)
	return success
end

if not collisionGroupExists(doorCollisionGroup) then
	PhysicsService:RegisterCollisionGroup(doorCollisionGroup)
end

local function setCollisionGroup(character, group, canCollide)
	for _, part in ipairs(character:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CollisionGroup = group
			part.CanCollide = canCollide
		end
	end
end

-- Function to handle character setup
local function onCharacterAdded(player, character)
	print("Character added for player:", player.Name)
	if player.Team and player.Team.Name == teamName then
		print("Setting collision group to", doorCollisionGroup, "for team", teamName)
		setCollisionGroup(character, doorCollisionGroup, false)
	else
		print("Setting collision group to Default")
		setCollisionGroup(character, "Default", true)
	end

	-- Continuously monitor and reapply the collision group
	spawn(function()
		while character.Parent do
			if player.Team and player.Team.Name == teamName then
				setCollisionGroup(character, doorCollisionGroup, false)
			else
				setCollisionGroup(character, "Default", true)
			end
			wait(0.1) -- Adjust the interval as needed
		end
	end)
end

local function onTeamChanged(player)
	print("Team changed for player:", player.Name)
	if player.Character then
		wait(0.1)
		onCharacterAdded(player, player.Character)
	end
end

local function onPlayerAdded(player)
	print("Player added:", player.Name)
	player.CharacterAdded:Connect(function(character)
		print("Character added event for player:", player.Name)
		character:WaitForChild("HumanoidRootPart")
		wait(0.1)
		onCharacterAdded(player, character)
	end)
	player:GetPropertyChangedSignal("Team"):Connect(function()
		onTeamChanged(player)
	end)
	if player.Character then
		print("Player already has character:", player.Name)
		wait(0.1)
		onCharacterAdded(player, player.Character)
	end
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in ipairs(game.Players:GetPlayers()) do
	onPlayerAdded(player)
end

It works! Thank you for your help, It is greatly appreciated, been trying to get this working all day :grinning: