Help With Parts Colliding

Hello fellow developers!

I don’t want to waste anyone’s time, so I will get straight to the point. Basically, I am running a script that makes the orange Part expand when I press a button. I want the orange Part to collide with the gray Parts, but because the script overrides the normal physics, it goes through the gray Parts. Here is a visual aid of what it looks like before any scripts are run.

If anyone could help me with this, I would be most grateful!

—JayR


Edit: Due to some previous mix-ups, I have decided to include a statement to clear some things up.

I am trying to make the middle orange part in the picture above collide with the four surrounding gray parts (as well as all other parts and players). Normally this would work, however, I have created a script that changes the part’s size when I click a button. This script overrides the normal physics, thus resulting in the middle orange part not colliding with the four surrounding gray blocks, but colliding with all other parts and players. Again, I want the middle orange part to collide with everything including players.

Hello, how are you doing?

Can’t you just turn the collision of the middle block off?

Great! Thanks for asking!

Are you talking about the CanCollide part of the orange block’s properties?

Yeah, turning it off should completely make the grey parts not be able to touch the middle part.

Actually turning the CanCollide to false allows me to walk right through the part, which is something I don’t want.

I see, then you can do something using collision groups, you can also make so players don’t collide with each other and even restricted team doors!

Hope i could help you!

2 Likes

So here is the script I swiped from one of the links you sent in your reply.

 local PhysicsService = game:GetService("PhysicsService")

 local Wraparound = "Wraparound"
 local Barriers = "Barriers"

 -- Create two collision groups
 PhysicsService:CreateCollisionGroup(Wraparound)
 PhysicsService:CreateCollisionGroup(Barriers)

 -- Add an object to each group
 PhysicsService:SetPartCollisionGroup(workspace.Wraparound_1, Wraparound)
 PhysicsService:SetPartCollisionGroup(workspace.Barrier_1, Barriers)
 PhysicsService:SetPartCollisionGroup(workspace.Barrier_2, Barriers)
 PhysicsService:SetPartCollisionGroup(workspace.Barrier_3, Barriers)
 PhysicsService:SetPartCollisionGroup(workspace.Barrier_4, Barriers)

 PhysicsService:CollisionGroupSetCollidable(Barriers, Wraparound, true)

Everything in the script looks fine to me, but for some reason it still doesn’t work (the orange Part still goes past the gray Parts).


Note: The script is modified to suit my needs, however, the original script looks almost exactly the same.

Change the middle block’s collision to true and change this:

into:

(Barriers, Wraparound, false)

I’m not that good with collision groups either, but i think this may work.

1 Like

The CanCollide for the middle part is already set to true, and changing the end part of the script to false doesn’t change anything. :frowning:

When I run the first script to change the part’s size, this is what I get.

If the grey blocks are anchored true, change them to anchored false. Is the part size script a local script or a server script?
Also, try adding workspace:WaitForChild(“BlockName”) to prevent any errors here:

1 Like

Doing that makes them just fall to ground and get destroyed. However, I found that if I change the middle part’s Anchored to true, then the part would collide with the gray parts. But in doing this, yet another problem arises. Here is a picture of what happens when I push the part. As you can see, the middle part is colliding with the gray parts, but it can be moved simply by pushing, which is something that I don’t want.

The part size script is a server script.

Doing that seems to have no affect.

I’ve never actually tried to use collision groups, let me see if i can improve your code. Give me some minutes for a bit of research.

1 Like

I’ve modified you’re code slightly, by changing the order of them.
Check if all of the blocks are collision true, and set the Wraparound block to anchored true, while the other parts to anchored false or true.

local PhysicsService = game:GetService("PhysicsService")

local MiddleBlock = "MiddleBlock"

local Barriers = "Barriers"

-- Create two collision groups

PhysicsService:CreateCollisionGroup(MiddleBlock)

PhysicsService:CreateCollisionGroup(Barriers)

-- Add an object to each group

PhysicsService:SetPartCollisionGroup(workspace.Wraparound_1, MiddleBlock)

PhysicsService:SetPartCollisionGroup(workspace.Barrier_1, Barriers)

PhysicsService:SetPartCollisionGroup(workspace.Barrier_2, Barriers)

PhysicsService:SetPartCollisionGroup(workspace.Barrier_3, Barriers)

PhysicsService:SetPartCollisionGroup(workspace.Barrier_4, Barriers)

PhysicsService:CollisionGroupSetCollidable(Barriers, MiddleBlock, false)

PhysicsService:CollisionGroupSetCollidable(MiddleBlock, Barriers, false)
1 Like

Unfortunately I still get the same result. Here is a picture of what happens.

As you can see in the picture, the middle part still goes through the gray parts.

I just noticed that you edited the post 5 times.
You changed what you initially wanted it to be?
Before it clearly specified you didn’t want the middle part to collide with the grey ones.

So let me get this straight:
You want the grey parts to collide with the middle block, but you don’t want any other stuff to collide with it?

1 Like

No, I think he wants it so that the middle one doesn’t collide with the greys.

Not sure.

This quote proves otherwise, he wants it to collide.

In my original, non-edited post, I stated “I am trying to get the orange block in the middle to not be able to touch the gray blocks” I agree, this sounds like I am trying to say that I want the middle part to not collide with the gray ones. However, I also stated “the gray blocks will act as barriers.” This is pretty clear as well that I do want the middle block to collide. I noticed later on that the statements were contradicting each other, and so I changed it for clarity’s sake.

Exactly.


Sorry for the mix up! I thought about letting you know that I changed the post; I should’ve listened to my gut and told you. :grimacing:

1 Like

Alright, sorry for all the misunderstanding, i now understand clearly what you’d like to do.

I may have found the solution for your problem.
I took some parts from the player not colliding with player script from the wiki, and transformed it into this:

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

local OtherBlocks = "OtherBlocks"

local Barriers = "Barriers"

-- Create two collision groups

PhysicsService:CreateCollisionGroup(Barriers)

-- Add an object to each group

PhysicsService:SetPartCollisionGroup(workspace.Wraparound_1, Barriers)

PhysicsService:CreateCollisionGroup(OtherBlocks)
PhysicsService:CreateCollisionGroup(OtherBlocks)
PhysicsService:CollisionGroupSetCollidable(OtherBlocks, Barriers, false)

local previousCollisionGroups = {}

local function setCollisionGroup(object)
	if object:IsA("BasePart") then
		previousCollisionGroups[object] = object.CollisionGroupId
		PhysicsService:SetPartCollisionGroup(object, OtherBlocks)
	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 = PhysicsService:GetCollisionGroupName(previousCollisionGroupId)
	if not previousCollisionGroupName then return end

	PhysicsService: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)

for i, v in pairs (game.Workspace:GetChildren()) do
	if v.Name ~= "Wraparound_1" and string.sub(v.Name, 1, 7) ~= "Barrier" then
		setCollisionGroup(v)
	end
end

This code should make so other parts inside the workspace and player characters not be able to collide with the middle part.

1 Like