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
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!
Actually i solved it myself, i cant add models to a collision group.