Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to wait until a specific collision group is added?

Asked by 5 years 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 2374 Moderation Voter Community Moderator
5 years 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 2374 — 5y
Ad

Answer this question