Want to stay connected to the Scripting Helpers community and get real-time help? Join the Scripting Helpers Discord Server!
Ad
Log in to vote
1

How to wait until a specific collision group is added?

Asked by 8 days ago

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

Log in to vote
0
Answered by
Avigant 1841
7 days ago

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.

0
Alternatively you could use a module that returns a table, and have both scripts check whether or not it was created that way. Avigant 1841 — 7d
Ad

Answer this question