Model inside a script

Hello I would like to know how can I do this for example, when a button is clicked, all the parts inside a model become can collide false, and when I click a another one they become can collide true, thanks in advance, God bless you all!

use if statements and for loops like this

for i,v in pairs(model:GetChildren()) do
--// check if  collision is enabled or disabled
end

for listening to button clicks u can use Activated or MouseButton1Click

1 Like

Add a LocalScript inside the button and paste this:

local button = script.Parent
local model = workspace:WaitForChild("Model") -- Replace "Model"  with the actual name of your model

button.MouseButton1Click:Connect(function()
	for _, v in (model:GetChildren()) do
		if v:IsA("BasePart") then
			v.CanCollide = false
		end
	end
end)
1 Like

is the button physical or GUI?


Things=workspace:FindFirstChild("Things")

function HeDidTheThing(mode)
	for _, v in (Things:GetChildren()) do
		if v:IsA("BasePart") then
			v.CanCollide = mode
		end
	end
end

--don't matter how you get to here.. mouseclick or whatever
--testing..

HeDidTheThing(false)
task.wait(5)
HeDidTheThing(true)

--keep in mind GetChildren() is only the root level
--if you want everything there use GetDescendants()

It’s not a gui, it’s a physical button

okay then here’s your answer
server script inside a button

local collide = true
script.Parent.MouseButton1Click:Connect(function()
  for i, v in pairs([path to model]):GetDescendants() do
    if v:IsA("Part") or v:IsA("MeshPart") then
      if collide = true then
        v.CanCollide = false
      else
        v.CanCollide = true
      end
    end
  end
  collide = not collide
end)

surely it can be optimized, but this is what id do
i edited it to be better

this can be written as

local collide = true

script.Parent.MouseButton1Click:Connect(function()
  collide = not collide
  for _, part in [path to model]:GetDescendants() do
    if part:IsA("BasePart") then
        part.CanCollide = collide
    end
  end
end)

Thank you, I’ll try it, what if I want to do it but also add a function that another model becomes can collide true when the button is clicked, so the script would make all the parts in model A become can collide false, and all the parts in group B become can collide true