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

Why are my collision groups with parts not working?

Asked by 4 years ago

I've just learnt about collision groups, i decided to make a script which i needed, for 2 specific parts, the players and a football. I want the football and player to not collide with each other.

This is my script:

local physicsService = game:GetService("PhysicsService")

physicsService:CreateCollisionGroup("Players")
physicsService:CreateCollisionGroup("Ball")

local ball = game.Workspace["Premier League Ball"]

physicsService:SetPartCollisionGroup(ball, "Ball")

physicsService:CollisionGroupSetCollidable("Players", "Ball", false)

game.Workspace.ChildAdded:Connect(function(plays)
    if plays:FindFirstChild("Humanoid") then
        print("yeshum")

        physicsService:SetPartCollisionGroup(plays, "Players")
    end
end)

I believe it is correct, but the player and ball still collide with each other:

https://gyazo.com/4e5e43a9a87aa4e938b21181e1af62d2

Help me out pls

Thx

2 answers

Log in to vote
2
Answered by 4 years ago

Okay, so you cannot set a model to a collision group. You can only add BaseParts to a collision group. I have answered something similar on https://scriptinghelpers.org/questions/94958/i-have-a-question-how-can-i-make-a-part-that-only-certain-other-parts-can-collide-with#87731 If you want, take a look at it.

So to do this you have to do it for every descendant.

workspace.ChildAdded:Connect(function(plays)

    if plays:FindFirstChild("Humanoid") then
        print("yeshum")

for k,part in next,plays:GetDescendants() do

if part:IsA("BasePart") then

        physicsService:SetPartCollisionGroup(part, "Players")

end

end

    end

end)

Done, but this is not accurate and is a bad practice. Why? This is because there can be dummies and NPCs on the workspace, it's better to use .CharacterAdded wich detects whenever a player gets a new character. This is good to use since it also gives the Character model to be used.

So to implement this you would have to do this

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)

Player.CharacterAdded:Connect(function(Character)

end)

end)

Now we script it so it adds to the Collision Group

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)

Player.CharacterAdded:Connect(function(Character)

for k,part in next,Character:GetDescendants() do

if part:IsA("BasePart") then --// Detect if the part is a BasePart

physicsService:SetPartCollisionGroup(part, "Players")

end

end

end)

end)

It should be done like that, now if you want you can add a descendantAdded event wich makes sure everything is set

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)

Player.CharacterAdded:Connect(function(Character)

for k,part in next,Character:GetDescendants() do

if part:IsA("BasePart") then --// Detect if the part is a BasePart

physicsService:SetPartCollisionGroup(part, "Players")

end

end

Character.DescendantAdded:Connect(function(Part)

if Part:IsA("BasePart") then

physicsService:SetPartCollisionGroup(Part, "Players")

end

end)

end)

end)

Done! Now it should be working like a charm!

This is the final code.

local physicsService = game:GetService("PhysicsService")

physicsService:CreateCollisionGroup("Players")
physicsService:CreateCollisionGroup("Ball")

local ball = game.Workspace["Premier League Ball"]

physicsService:SetPartCollisionGroup(ball, "Ball")

physicsService:CollisionGroupSetCollidable("Players", "Ball", false)

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)

Player.CharacterAdded:Connect(function(Character)

for k,part in next,Character:GetDescendants() do

if part:IsA("BasePart") then --// Detect if the part is a BasePart

physicsService:SetPartCollisionGroup(part, "Players")

end

end

Character.DescendantAdded:Connect(function(Part) --// Incase something else gets added

if Part:IsA("BasePart") then

physicsService:SetPartCollisionGroup(Part, "Players")

end

end)

end)

end)

Have fun!

If this answered yor question accept the answer!

Ad
Log in to vote
0
Answered by 4 years ago

Actually i solved it myself, i cant add models to a collision group.

0
May you check my answer uh, please? i spent some time on it maumaumaumaumaumua 628 — 4y
0
please accept it aa i spent time on it AAAAA maumaumaumaumaumua 628 — 4y

Answer this question