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

How to wait until a specific collision group is added?

In two different scripts, I'm creating a collision group. The objective is making it so that the collision groups made in each script don't collide with each other. The problem is that they both run at the same time meaning there is a chance the other group won't be created yet when it tells the server to make each group not be able to collide with each other.

How would you check if the collision group is already made or not?

1 answer

✓ Accepted answer

Well, technically speaking, scripts don't run at the same time. They run consecutively, and the order is determined by the thread scheduler. But aside from that, here's one option:

local Services = {
	Physics = game:GetService("Physics")
}

local function IsCollisionGroupExisting(Name)
	local IsSuccess = pcall(function()
		return Services.Physics:GetCollisionGroupId(Name)
	end)

	return IsSuccess
end

local Name = "Test"

if not IsCollisionGroupExisting(Name) then
	Services.Physics:CreateCollisionGroup(Name)
end

Of course, a much better approach would be to use one Script and one LocalScript for your entire game, and have all of your code rest in modules, so you don't get potential threading bugs like this one.

1 comment

0

Alternatively you could use a module that returns a table, and have both scripts check whether or not it was created that way.

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