Need help with the anti-collide script

How can I get the script:
local PhysicsService = game:GetService(“PhysicsService”)

PhysicsService:CreateCollisionGroup(“RaceCars”)
PhysicsService:CollisionGroupSetCollidable(“RaceCars”, “RaceCars”, false)

local function setCollisionGroupRecursive(object)
if object:IsA(“BasePart”) then
PhysicsService:SetPartCollisionGroup(object, “RaceCars”)
end
for _, child in ipairs(object:GetChildren()) do
setCollisionGroupRecursive(child)
end
end

local function onCharacterAdded(character)
setCollisionGroupRecursive(character)
end

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

game.Players.PlayerAdded:Connect(onPlayerAdded)

to work when I spawn a car thats located in replicatedstorage.

When I use my spawn GUI to spawn the car only the main mesh part of the car is uncollidable with the player and the same car. The interior and wheels are still collidable. When the car is already in the workspace when the game is launched the script works as intended.

try using GetDescendants() instead of GetChildren()

Worked, thanks for the solution dude!

1 Like

Hey dude, your fixed worked just fine but when I came back into the game it wasn’t working again?? I hadn’t touched any of the code prior I just went straight into testing and it didn’t work.

It seems the issue might be that some parts of the character aren’t fully loaded when the setCollisionGroupRecursive function is triggered. One solution would be to use the DescendantAdded connection to ensure nothing is missed, like this:

local function setCollisionGroupRecursive(object)
    if object:IsA("BasePart") then
        PhysicsService:SetPartCollisionGroup(object, "RaceCars")
    end
    for _, child in object:GetDescendants() do
        setCollisionGroupRecursive(child)
    end
    object.DescendantAdded:Connect(function(child)
        setCollisionGroupRecursive(child)
    end)
end

But now that I look at it, your code already calls setCollisionGroupRecursive every time a child is found. Since we’re working with descendants, I don’t think the recursive call is necessary anymore. So, I suggest this version:

local function setCollisionGroupRecursive(object)
    if object:IsA("BasePart") then
        object.CollisionGroup = "RaceCars"
    end
    for _, child in object:GetDescendants() do
        if not child:IsA("BasePart") then continue end
        child.CollisionGroup = "RaceCars"
    end
    object.DescendantAdded:Connect(function(child)
        if not child:IsA("BasePart") then return end
        child.CollisionGroup = "RaceCars"
    end)
end

Also, you can remove ipairs since it’s no longer necessary.

I tried both of these different scripts but it hasn’t corrected the issue. I’m using this script on two cars and the one car that has its primary part as a mesh part is not collidable which is good but the rest of the car is. The other car which has the drive seat as the primary part is fully collidable. This might help give some insight. I’m not getting any errors in the output just deprecated warnings which shouldn’t effect the script in any way.

I noticed that you’re using PhysicsService:SetPartCollisionGroup(), which is deprecated. Instead, you should use:

object.CollisionGroup = "RaceCars"

This directly sets the collision group for the part without needing to call the PhysicsService method. Excuse me! (I edited the second script)

This is my script for that … ServerScript on the root of the car in storage. When it’s spawned it fires.

local phs = game:GetService("PhysicsService")

phs:RegisterCollisionGroup("Car")
phs:CollisionGroupSetCollidable("Car", "Car", false)
for _, part in pairs(script.Parent:GetDescendants()) do
	if part:IsA("BasePart") then
		part.CollisionGroup = "Car"
	end
end

Was messing around with this one…

local phs = game:GetService("PhysicsService")
local players = game:GetService("Players")

phs:RegisterCollisionGroup("Car")
phs:RegisterCollisionGroup("Player")
phs:CollisionGroupSetCollidable("Car", "Car", false)
phs:CollisionGroupSetCollidable("Car", "Player", false)

for _, part in pairs(script.Parent:GetDescendants()) do
	if part:IsA("BasePart") then
		part.CollisionGroup = "Car"
	end
end

for _, player in pairs(players:GetPlayers()) do
	local char = player.Character
	if char then
		for _, part in pairs(char:GetDescendants()) do
			if part:IsA("BasePart") then
				part.CollisionGroup = "Player"
			end
		end
	end
end

But it may be better to use something like this for that;
script.Parent.Seat.ChildAdded:Connect(function(occupant)

The script you sent without the warns straight up just doesn’t work :(. The original script I sent at the start of the post would work when the car was placed straight into the workspace but didn’t work when it was spawned from replicated storage. Also the mesh part had cancollide unticked for some odd reason so that it why you couldn’t collide with it.

I tried this and got the same results as just now.

Odd been working for me for a long time …

Chevy01
   Collision (script)

Right on the root of the car.

Yo it worked thanks a lot!! Finally fixed

1 Like