-- NinjoOnline -- local physicsService = game:GetService("PhysicsService") local replicatedStorage = game:GetService("ReplicatedStorage") local valuesFolder = replicatedStorage:WaitForChild("ValuesFolder") local activeValue = valuesFolder:WaitForChild("ActiveValue") local blueDoor = workspace:WaitForChild("Castle Map").BlueCastle.Hospital.HospitalDoor local redDoor = workspace:WaitForChild("Castle Map").RedCastle.Hospital.HospitalDoor local blueForceFields = "BlueForceFields" local redForceFields = "RedForceFields" local bluePlayers = "BluePlayers" local redPlayers = "RedPlayers" physicsService:CreateCollisionGroup(blueForceFields) physicsService:CreateCollisionGroup(redForceFields) physicsService:CreateCollisionGroup(bluePlayers) physicsService:CreateCollisionGroup(redPlayers) physicsService:SetPartCollisionGroup(blueDoor, blueForceFields) physicsService:SetPartCollisionGroup(redDoor, redForceFields) physicsService:CollisionGroupSetCollidable(blueForceFields, bluePlayers, false) physicsService:CollisionGroupSetCollidable(redForceFields, redPlayers, false) local function setCollisionGroupRecursive(object, groupName) if object:IsA("BasePart") then physicsService:SetPartCollisionGroup(object, groupName) end for _, child in ipairs(object:GetChildren()) do setCollisionGroupRecursive(child, groupName) end end function onCharacterAdded(character, team) local collisionGroupName = team.Name .. "Players" setCollisionGroupRecursive(character, collisionGroupName) end game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:Connect(function(character) while wait() do if tostring(player.Team) ~= "Lobby" then onCharacterAdded(character, player.Team) break end end end) end) activeValue.Changed:connect(function(value) if value == false then -- Remove players from group end end)
Right now I have a collision group script which sets players to be able to walk through their own team doors, without being able to walk through the opposing teams doors. I got most of this from the wiki. The problem I am facing however, is 1. It only sets the players collision group when their character respawns and 2. It carries over from round to round. I have a section where if the round is not in progress then I want to clear all collision group, but I want when a new round starts for the new collision groups to be setup according to each players team. Can anyone help me, because I cant't find anywhere how to remove players from a collision group on any of the wikis
If you investigate the SetPartCollisionGroup function, you can realize that all it does is configure each Part's CollisionGroupId. This means that any one part can only belong to a single CollisionGroup at any one time. Thus, to get a player out of a CollisionGroup, simply assign all their parts' CollisionGroupId to 0 or call SetPartCollisionGroup(part, "Default")
(http://wiki.roblox.com/index.php?title=API:Class/PhysicsService/CreateCollisionGroup suggests that is the name reserved for group id 0).
I recommend this code for PlayerAdded:
function CollisionNameFromTeam(team) return team.Name .. "Players" end function CharacterValid(char) return char and char:FindFirstChild("Humanoid") and char.Humanoid.Health > 0 end function SetProperCollisionGroup(p) if p.Team and p.Team.Name ~= "Lobby" then setCollisionGroupRecursive(p.Character, CollisionNameFromTeam(p.Team)) else setCollisionGroupRecursive(p.Character, "Default") end end game.Players.PlayerAdded:Connect(function(p) p.CharacterAdded:Connect(function(char) SetProperCollisionGroup(p) end) p:GetPropertyChangedSignal("Team"):Connect(function() if not CharacterValid(p.Character) then return end SetProperCollisionGroup(p) end) end)
Instead of wait
ing for the Team to become correct, it uses events to make sure that regardless of respawning/changing teams, the collision group will always be correct.