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

How can i fix explosions still colliding with objects even with collision groups set up?

Asked by 3 years ago

So here's the issue

I have a game that uses a custom explosion system so i can have more control over how it works. Recently i have tried to make it so players and objects that do not need to collide with explosions not collide with them.

But this doesn't seem to be working at all. Everything is still colliding with the explosions.

Code below

Explosion Module

local module = {}

function module:Explode(position,radius,damage,outercolor,innercolor)
    local map
    for i,v in pairs(workspace:GetChildren()) do
        if game.ServerStorage.Maps:FindFirstChild(v.Name) then
            map = v
        end
    end
    local explosionclone = game.ServerStorage.OuterExplosion:Clone()
    if outercolor ~= nil then
        explosionclone.Color = outercolor
    end
    local audio = game.ServerStorage.ExplosionAudio:Clone()
    audio.Parent = explosionclone
    audio.RollOffMinDistance = radius*2
    audio.RollOffMaxDistance = radius*5
    explosionclone.Size = Vector3.new(radius*2,radius*2,radius*2)
    explosionclone.Velocity = Vector3.new(radius,radius,radius)
    explosionclone.Position = position
    local iexplosionclone = game.ServerStorage.InnerExplosion:Clone()
    if innercolor ~= nil then
        iexplosionclone.Color = innercolor
    end
    iexplosionclone.Size = Vector3.new(radius*1.6,radius*1.6,radius*1.6)
    iexplosionclone.Position = position
    iexplosionclone.Velocity = Vector3.new(radius,radius,radius)
    explosionclone.Parent = workspace.Explosions
    iexplosionclone.Parent = workspace.Explosions
    audio:Play()
    for i,v in pairs(game.Players:GetChildren()) do
        if workspace:FindFirstChild(v.Name) then
            if (v.Character.HumanoidRootPart.Position-position).magnitude <= radius and v.Character.Humanoid.Health >= 1 and v.Dead.Value == false then
                v.Character.Humanoid.Health = v.Character.Humanoid.Health - damage
                if v.Character.Humanoid.Health <= 0 then
                    game.ReplicatedStorage.ChatEvent:FireAllClients(v.Name.." got blown up.")
                end
            end
        end
    end
    for i,v in pairs(map.Map:GetChildren()) do
        if v.ClassName == "Part" or v.ClassName == "WedgePart" or v.ClassName == "MeshPart" or v.ClassName == "UnionOperation" then
            if (v.Position-position).magnitude <= radius then
                local rng = math.random(1,10)
                v.Anchored = false
                if rng >= 6 then
                    v:ClearAllChildren()
                end
                if rng >= 9 then
                    v:Destroy()
                end
            end 
        end
    end
end

return module

Collision Management

local ps = game:GetService("PhysicsService")

ps:CreateCollisionGroup("Objects")
ps:CreateCollisionGroup("Enemies")
ps:CreateCollisionGroup("Explosions")

local Players = game:GetService("Players")

local playerCollisionGroupName = "Players"
ps:CreateCollisionGroup(playerCollisionGroupName)
ps:CollisionGroupSetCollidable(playerCollisionGroupName, playerCollisionGroupName, false)

local previousCollisionGroups = {}

local function setCollisionGroup(object)
    if object:IsA("BasePart") then
        previousCollisionGroups[object] = object.CollisionGroupId
        ps:SetPartCollisionGroup(object, playerCollisionGroupName)
    end
end

local function setCollisionGroupRecursive(object)
    setCollisionGroup(object)

    for _, child in ipairs(object:GetChildren()) do
        setCollisionGroupRecursive(child)
    end
end

local function resetCollisionGroup(object)
    local previousCollisionGroupId = previousCollisionGroups[object]
    if not previousCollisionGroupId then return end 

    local previousCollisionGroupName = ps:GetCollisionGroupName(previousCollisionGroupId)
    if not previousCollisionGroupName then return end

    ps:SetPartCollisionGroup(object, previousCollisionGroupName)
    previousCollisionGroups[object] = nil
end

local function onCharacterAdded(character)
    setCollisionGroupRecursive(character)

    character.DescendantAdded:Connect(setCollisionGroup)
    character.DescendantRemoving:Connect(resetCollisionGroup)
end

local function onPlayerAdded(player)
    player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

ps:CollisionGroupsAreCollidable("Objects","Explosions",false)
ps:CollisionGroupsAreCollidable("Players","Explosions",false)
ps:CollisionGroupsAreCollidable("Enemies","Explosions",false)

workspace.ActiveDisasters.ChildAdded:Connect(function(child)
    if child.ClassName == "Model" then
        for i,v in pairs(child:GetChildren()) do
            if v.ClassName == "Part" or v.ClassName == "MeshPart" or v.ClassName == "UnionOperation" or v.ClassName == "WedgePart" then
                if child:FindFirstChild("Humanoid") then
                    ps:SetPartCollisionGroup(v,"Enemies")
                else
                    ps:SetPartCollisionGroup(v,"Objects")
                end
            end
        end
    elseif child.ClassName == "Part" or child.ClassName == "MeshPart" or child.ClassName == "UnionOperation" or child.ClassName == "WedgePart" then
        ps:SetPartCollisionGroup(child,"Objects")
    end
end)

workspace.Explosions.ChildAdded:Connect(function(child)
    ps:SetPartCollisionGroup(child,"Explosions")
end)

How i have tried to solve the issue:

Moved the setting part to the collision group for explosions into the explosion module (Didn't work, same issue occurred) Took the velocity out of the explosions (Didn't work, same issue occurred)

Yes I am aware Velocity is deprecated now, the module was made before it was deprecated.

Answer this question