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 5 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:

01local physicsService = game:GetService("PhysicsService")
02 
03physicsService:CreateCollisionGroup("Players")
04physicsService:CreateCollisionGroup("Ball")
05 
06local ball = game.Workspace["Premier League Ball"]
07 
08physicsService:SetPartCollisionGroup(ball, "Ball")
09 
10physicsService:CollisionGroupSetCollidable("Players", "Ball", false)
11 
12game.Workspace.ChildAdded:Connect(function(plays)
13    if plays:FindFirstChild("Humanoid") then
14        print("yeshum")
15 
16        physicsService:SetPartCollisionGroup(plays, "Players")
17    end
18end)

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 5 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.

01workspace.ChildAdded:Connect(function(plays)
02 
03    if plays:FindFirstChild("Humanoid") then
04        print("yeshum")
05 
06for k,part in next,plays:GetDescendants() do
07 
08if part:IsA("BasePart") then
09 
10        physicsService:SetPartCollisionGroup(part, "Players")
11 
12end
13 
14end
15 
16    end
17 
18end)

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

1local Players = game:GetService("Players")
2 
3Players.PlayerAdded:Connect(function(Player)
4 
5Player.CharacterAdded:Connect(function(Character)
6 
7end)
8 
9end)

Now we script it so it adds to the Collision Group

01local Players = game:GetService("Players")
02 
03Players.PlayerAdded:Connect(function(Player)
04 
05Player.CharacterAdded:Connect(function(Character)
06 
07for k,part in next,Character:GetDescendants() do
08 
09if part:IsA("BasePart") then --// Detect if the part is a BasePart
10 
11physicsService:SetPartCollisionGroup(part, "Players")
12 
13end
14 
15end
16 
17end)
18 
19end)

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

01local Players = game:GetService("Players")
02 
03Players.PlayerAdded:Connect(function(Player)
04 
05Player.CharacterAdded:Connect(function(Character)
06 
07for k,part in next,Character:GetDescendants() do
08 
09if part:IsA("BasePart") then --// Detect if the part is a BasePart
10 
11physicsService:SetPartCollisionGroup(part, "Players")
12 
13end
14 
15end
View all 29 lines...

Done! Now it should be working like a charm!

This is the final code.

01local physicsService = game:GetService("PhysicsService")
02 
03physicsService:CreateCollisionGroup("Players")
04physicsService:CreateCollisionGroup("Ball")
05 
06local ball = game.Workspace["Premier League Ball"]
07 
08physicsService:SetPartCollisionGroup(ball, "Ball")
09 
10physicsService:CollisionGroupSetCollidable("Players", "Ball", false)
11 
12local Players = game:GetService("Players")
13 
14Players.PlayerAdded:Connect(function(Player)
15 
View all 40 lines...

Have fun!

If this answered yor question accept the answer!

Ad
Log in to vote
0
Answered by 5 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 — 5y
0
please accept it aa i spent time on it AAAAA maumaumaumaumaumua 628 — 5y

Answer this question