I want to make a script where only a specific team can pass the part but without touching it, but only this team can pass and no one else

-- Assuming this script is placed in a part or object in the game

-- Reference to the "Ultras" team
local Ultras = game.Teams.Ultras

-- Function to check and update CanCollide
local function updateCollide()
    -- Loop through all players in the game
    for _, player in pairs(game.Players:GetPlayers()) do
        -- Check if the player is on the "Ultras" team
        if player.Team == Ultras then
            -- Set CanCollide to false for the part
            script.Parent.CanCollide = false
            return  -- Exit the function since we only need one "Ultras" player to disable collision
        end
    end

    -- If no "Ultras" player is found, set CanCollide to true
    script.Parent.CanCollide = true
end

-- Connect the Heartbeat event to the updateCollide function
game:GetService("RunService").Heartbeat:Connect(updateCollide)

So Here If someone’s team is Ultras everyone can pass, I just want it passable only for Ultras and no one else.

2 Likes

You can use collision groups to achieve this:

--script in ServerScriptService
local Ultras = game.Teams.Ultras
local bricks = {} -- insert all of the bricks that can only be walked through by the Ultra players

local PhysicsService = game:GetService("PhysicsService")
PhysicsService:RegisterCollisionGroup("UltraPlayers")
PhysicsService:RegisterCollisionGroup("UltraParts")
PhysicsService:CollisionGroupSetCollidable("UltraPlayers", "UltraParts", false) 
-- set Ultra parts uncollidible with Ultra players

local function SetCollisionGroup(char, team) -- sets character's collision group
	for i,v in pairs(char:GetDescendants()) do -- loop through character's parts
		if v:IsA("BasePart") then 
			v.CollisionGroup = team -- set collision group to given string
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr) -- player added
	if plr.Character and plr.Team == Ultras then 
		SetCollisionGroup(plr.Character, "UltraPlayers") 
	end
	plr.CharacterAdded:Connect(function(char) -- set group when player respawns
		if plr.Team == Ultras then
			SetCollisionGroup(char, "UltraPlayers")
		end
	end)
	plr:GetPropertyChangedSignal("Team"):Connect(function() -- set group when player changes team
		if plr.Team == Ultras then
			SetCollisionGroup(plr.Character, "UltraPlayers")
		else SetCollisionGroup(plr.Character, "Default") -- normal collision group
		end
	end)
end)

for i,v in pairs(bricks) do -- set parts' collision groups
	v.CollisionGroup = "UltraParts"
end
5 Likes

Thank you so much, It works thank you for helping me :star_struck:

3 Likes

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