Hello there, I am Ulric. I am here to ask you how to create team doors with Physics Service. I tried using the documentation, but it was no use. Can anyone show me a simple script of team door using Physics Service.
This is a simple example of how you would do Team Doors using PhysicsService
on the right way. We do several checks to ensure everything goes as expected and to properly manage CollisionGroups.
First, We’re gonna create 2 Collision Groups for the Blue Team - One for the Players’ Character’s and one for the Door. The same goes for the Red Team.
After this, We create a function that will loop through the Reference
(Instance) provided, Apply the CollisionGroup
into it and do the same for it’s Children. (Note that only BaseParts can have CollisionGroups)
For last, All we need to do is to simply create a function that’ll run everytime a Player is added, Listen to whenever the Player’s Character gets added, run the function to apply Collision Groups into the Character’s Descendants and Connect it to the PlayerAdded
Event, As well as setting up Collisions for each Teams’ Door. (We can’t forget to also run this for Players that have already joined the game)
-- Services
local PhysicsService = game:GetService("PhysicsService")
local TeamService = game:GetService("Teams")
local Players = game:GetService("Players")
-- Teams
local RedTeam = TeamService:WaitForChild("Red")
local BlueTeam = TeamService:WaitForChild("Blue")
-- Doors (Change them according to where you placed each Door)
local RedTeamDoor = workspace.RedTeamDoor
local BlueTeamDoor = workspace.BlueTeamDoor
-- Creating Collision Groups.
PhysicsService:CreateCollisionGroup("RedTeamCollision")
PhysicsService:CreateCollisionGroup("BlueTeamCollision")
PhysicsService:CreateCollisionGroup("RedDoorCollision")
PhysicsService:CreateCollisionGroup("BlueDoorCollision")
-- Setting Collision Groups, Disable RedTeamCollision with RedDoorCollision;
-- And BlueTeamCollision with BlueDoorCollision.
PhysicsService:CollisionGroupSetCollidable(
"RedTeamCollision",
"RedDoorCollision",
false
)
PhysicsService:CollisionGroupSetCollidable(
"BlueTeamCollision",
"BlueDoorCollision",
false
)
local function SetupCollisionGroup(Reference: Instance, CollisionGroup: string)
-- Ensure the Reference (Instance) is a BasePart and if it's already inside CollisionGroup.
-- We do this to avoid running the same code several times twice.
local IsBasePart = Reference:IsA("BasePart")
local InCollisionGroup = IsBasePart and PhysicsService:CollisionGroupContainsPart(
CollisionGroup,
Reference
)
if InCollisionGroup then
return
end
-- Get the Reference's Children and check if we have enough to iterate.
-- We also are already gonna set the Reference's CollisionGroup as by now we know it doesn't has one.
local ReferenceChildren = Reference:GetChildren()
local HasEnoughChildren = ReferenceChildren and table.getn(ReferenceChildren) > 0
if IsBasePart then
PhysicsService:SetPartCollisionGroup(
Reference,
CollisionGroup
)
end
if not HasEnoughChildren then
return
end
-- Iterate through Reference's Children and recursively apply the same method.
for _, Child in ipairs(ReferenceChildren) do
SetupCollisionGroup(
Child,
CollisionGroup
)
end
end
local function OnPlayerAdded(Player: Player)
Player.CharacterAdded:Connect(function(Character: Model)
-- We're gonna defer this to ensure the Character is actually added;
-- And also to avoid some small issues upon running the function.
task.defer(function()
-- If the Player is on the Red Team, CollisionGroup will be equals to the Red Team's one;
-- Else, Blue Team's.
local IsOnRedTeam = (Player.Team == RedTeam)
local IsOnBlueTeam = (Player.Team == BlueTeam)
local CollisionGroup = if IsOnRedTeam then "RedTeamCollision" elseif IsOnBlueTeam then "BlueTeamCollision" else nil
if not CollisionGroup then
return
end
SetupCollisionGroup(
Character,
CollisionGroup
)
end)
end)
end
-- Setup collisions and Connect the OnPlayerAdded function.
SetupCollisionGroup(RedTeamDoor, "RedDoorCollision")
SetupCollisionGroup(BlueTeamDoor, "BlueDoorCollision")
Players.PlayerAdded:Connect(OnPlayerAdded)
-- Initialize the PlayerAdded function in-case there's already Players in-game.
for _, Player in ipairs(Players:GetPlayers()) do
task.spawn(OnPlayerAdded, Player)
end
thanks man! You have really helped me out! For a while now i’ve been trying to make something like this