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