How are these codes conflicting?

Here is the TeamDoor Script

local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local doorCollisionGroup = "GroupDoor"
local targetTeams = {"Grand Chancellor", "Patron", "Imperial Patron", "Executive Director"} -- Add more team names as needed

-- Ensure the door collision group exists and configure its collision rules
PhysicsService:RegisterCollisionGroup(doorCollisionGroup)
PhysicsService:CollisionGroupSetCollidable(doorCollisionGroup, doorCollisionGroup, false)

-- Set the collision group for the door parts (replace "GroupDoor" with your actual door part name)
for _, part in ipairs(workspace.GroupDoor:GetDescendants()) do
	if part:IsA("BasePart") then
		part.CollisionGroup = doorCollisionGroup
	end
end

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

local function isInTargetTeams(teamName)
	for _, targetTeam in ipairs(targetTeams) do
		if teamName == targetTeam then
			return true
		end
	end
	return false
end

local function onCharacterAdded(player, character)
	local team = player.Team
	if team and isInTargetTeams(team.Name) then
		setCollisionGroupForCharacter(character, doorCollisionGroup)
	else
		setCollisionGroupForCharacter(character, "Default")
	end
end

local function onPlayerAdded(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		character:WaitForChild("HumanoidRootPart")
		onCharacterAdded(player, character)
	end)

	player:GetPropertyChangedSignal("Team"):Connect(function()
		if player.Character then
			onCharacterAdded(player, player.Character)
		end
	end)

	if player.Character then
		onCharacterAdded(player, player.Character)
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)

-- Handle players who are already in the game
for _, player in ipairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end

Here is the GroupDoor script

local PhysicsService = game:GetService("PhysicsService")

local doorCollisionGroup = "TeamDoor"

-- Register the collision group
PhysicsService:RegisterCollisionGroup(doorCollisionGroup)
PhysicsService:CollisionGroupSetCollidable(doorCollisionGroup, doorCollisionGroup, false)

-- Set collision group for the door
for _, part in pairs(workspace.TeamDoor:GetDescendants()) do
	if part:IsA("BasePart") then
		part.CollisionGroup = doorCollisionGroup
	end
end

-- Player added event
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if player:GetRankInGroup(34574484) >= 1 then
			for _, part in pairs(character:GetDescendants()) do
				if part:IsA("BasePart") then
					part.CollisionGroup = doorCollisionGroup
				end
			end
		end
	end)
end)

The GroupDoor Script works when player.CharacterAdded:Connect(function(character) is changed to player.CharacterApperanceLoaded:Connect(function(character) but this causes the TeamDoor to stop working as team door also uses CharacterApperanceLoaded.

1 Like

Hey Moose!

The conflict is because of both scripts using CharacterAppearanceLoaded for different purposes, due to GroupDoor consistently relying on CharacterAdded. You can do this with the GroupDoor script, too. Here is the modified version:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("HumanoidRootPart")
        if player:GetRankInGroup(34574484) >= 1 then
for _, part in pairs(character:GetDescendants()) do
                if part:IsA("BasePart") then
                    part.CollisionGroup = doorCollisionGroup
end
            end
        end
    end)
end)

Hopefully, this will avoid the conflict and have the scripts run smoothly.

Thank you for trying to fix it, It didn’t seem to work though I’m afraid, It only seems to work when it’s loaded after the player has finished loading fully.

1 Like

You can use Humanoid’s LoadCharacter event instead, ensuring the character is fully loaded before applying changes:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").CharacterAppearanceLoaded:Connect(function()
            if player:GetRankInGroup(34574484) >= 1 then
                for _, part in pairs(character:GetDescendants()) do
                    if part:IsA("BasePart") then
                        part.CollisionGroup = doorCollisionGroup
                    end
                end
            end
        end)
    end)
end)

This should trigger after the character is fully loaded. Give it a shot and let me know how it goes.

It throws up this error

Ah, I see.
The issue arises because both scripts are using CharacterAppearanceLoaded for different purposes, causing a conflict. Here’s a solution:

For the GroupDoor script, stick with CharacterAdded, but ensure the character is fully loaded before applying changes:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").CharacterAppearanceLoaded:Connect(function()
            if player:GetRankInGroup(34574484) >= 1 then
                for _, part in pairs(character:GetDescendants()) do
                    if part:IsA("BasePart") then
                        part.CollisionGroup = doorCollisionGroup
                    end
                end
            end
        end)
    end)
end)

This should ensure the character is fully loaded before setting the collision group. Give it a try and let me know if it works.

Is that suppose to be a different script may I ask, As it’s the same as posted before?

Yeahh, My brain isn’t braining. Give me a moment to see the docs and such.

The team door seems to be good, you don’t need to fix it I think- But you can try this.

local PhysicsService = game:GetService("PhysicsService")
local doorCollisionGroup = "GroupDoor"

-- Register the collision group (IF, I recall how to do it. I'm not at my development PC right now so I can't test it.)
PhysicsService:RegisterCollisionGroup(doorCollisionGroup)
PhysicsService:CollisionGroupSetCollidable(doorCollisionGroup, doorCollisionGroup, false)

-- Set collision group for the door
for _, part in pairs(workspace.TeamDoor:GetDescendants()) do
	if part:IsA("BasePart") then
		part.CollisionGroup = doorCollisionGroup
	end
end

-- Player added event 
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("HumanoidRootPart")
		character:WaitForChild("Humanoid").CharacterAppearanceLoaded:Connect(function()
			if player:GetRankInGroup(34574484) >= 1 then
				for _, part in pairs(character:GetDescendants()) do
					if part:IsA("BasePart") then
						part.CollisionGroup = doorCollisionGroup
					end
				end
			end
		end)
	end)
end)

Doesn’t work I’m afraid, Still throws up this error

Alright, I fell asleep so sorry- But here’s something that I think’ll work.

local PhysicsService = game:GetService("PhysicsService")
local doorCollisionGroup = "GroupDoor"

-- Register the collision group
PhysicsService:RegisterCollisionGroup(doorCollisionGroup)
PhysicsService:CollisionGroupSetCollidable(doorCollisionGroup, doorCollisionGroup, false)

-- Set collision group for the door
for _, part in pairs(workspace.TeamDoor:GetDescendants()) do
	if part:IsA("BasePart") then
		part.CollisionGroup = doorCollisionGroup
	end
end

-- Player added event
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("HumanoidRootPart")
		player.CharacterAppearanceLoaded:Connect(function() -- not humanoid, I think it's a valid member of player if I recall.
			if player:GetRankInGroup(34574484) >= 1 then
				for _, part in pairs(character:GetDescendants()) do
					if part:IsA("BasePart") then
						part.CollisionGroup = doorCollisionGroup
					end
				end
			end
		end)
	end)
end)

Sorry for the late reply, Also doesn’t seem to be working. But it’s not giving any errors

1 Like