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