Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Clearing and restarting collision group problem?

-- 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

1 comment

0

After a round you could change all of the players team to lobby then just reset the character? There are also events in a team object for when a player is added / removed from that team which you could also use.

1 answer

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 waiting 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.

5 comments

0

It works for the doors, but I've noticed now that unions are CanCollide = false, even if the property is ticked as true

0

Double check you've configured the Collision Groups correctly? If it's in a group where it sets to to CanCollide = false with other stuff, that'd explain it.

0

The unions have the collision group as default and the ID of 0. Idk what it should be

0

CollisionFidelity is default, sorry*

0

Sorry, I have no idea what's going on. Try other CollisionFidelity options if you like. If you can reproduce the problem with as little other stuff as possible (in an otherwise blank place), you could use that in a bug report to Roblox.

This discussion is preserved as it was. New answers, comments, and votes are closed.