If we’re out here Optimizing it another potential issue is if you have models underneath other models we can use recursion to make sure that that won’t be an issue.
local button = script.Parent.Parent.TextButton
local button2 = script.Parent.Parent.TextButton2
local button3 = script.Parent.Parent.TextButton3
local selectedColour = script.Parent.Colour.TextLabel
local partsToModify = workspace:WaitForChild("PartsFolder")
RecursionFunction = function(Model)
for i, v in pairs(Model:GetChildren()) do
if v:IsA("BasePart") then
v.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
elseif v:IsA("Model") then
RecursionFunction(Model)
end
end
end
button3.MouseButton1Down:Connect(RecursionFunction(partsToModify))
button2.MouseButton1Down:Connect(function()
local lost = workspace:WaitForChild("lost")
local text = lost.SurfaceGui.TextLabel
text.TextColor3 = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
end)
The :GetDescendants() method eliminates the need for recursion. The idea is the same, that you wish to reach all levels deep within the Folder, which :GetDescendants() solves by returning a table of all descendants (children of children of children, etc.) of the object.
As ChiefWildin said, :GetDescendants() gets everything in the place you indicate, even if there are 15 folders inside a folder. Return everything. Everything.
Ok, what if I wanted to have two different buttons that set the color to different colors? Like a Primary and Secondary, would I use the same code? Or would it vary.
You can still reuse the same code, just pass in different arguments. Something like the following:
local button = script.Parent.Parent.TextButton
local button2 = script.Parent.Parent.TextButton2
local button3 = script.Parent.Parent.TextButton3
local selectedColour = script.Parent.Colour.TextLabel
local partsToModify = workspace:WaitForChild("PartsFolder")
function modifyParts(newColor)
for i, v in pairs(partsToModify:GetDescendants()) do
if v:IsA("BasePart") then
v.Color = newColor
end
end
end
button3.MouseButton1Down:Connect(function()
modifyParts(Color3.fromRGB(table.unpack(selectedColour.Text:split(", "))))
end)
button.MouseButton1Down:Connect(function()
modifyParts(Color3.new(1,1,1))
end)
button2.MouseButton1Down:Connect(function()
local lost = workspace:WaitForChild("lost")
local text = lost.SurfaceGui.TextLabel
text.TextColor3 = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
end)
button and button3 now re-use the same code, but pass in different colors.
local button = script.Parent.Parent.TextButton
local button2 = script.Parent.Parent.TextButton2
local button3 = script.Parent.Parent.TextButton3
local selectedColour = script.Parent.Colour.TextLabel
local partsToModify = workspace:WaitForChild("PartsFolder")
function modifyParts(newColor)
for i, v in pairs(partsToModify:GetDescendants()) do
if v:IsA("BasePart") then
v.Color = newColor
end
end
end
button3.MouseButton1Down:Connect(function()
modifyParts(Color3.fromRGB(table.unpack(selectedColour.Text:split(", "))))
end)
button.MouseButton1Down:Connect(function()
modifyParts(Color3.fromRGB(table.unpack(selectedColour.Text:split(", "))))
end)
button2.MouseButton1Down:Connect(function()
local lost = workspace:WaitForChild("lost")
local text = lost.SurfaceGui.TextLabel
text.TextColor3 = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
end)
Exactly, you should change it as you have shown it to us. If you changed it, and it doesn’t work, let us know.
Wait, you want to change the primary and secondary colors?
In this case, you’re assigning both buttons to do the same thing. For what you’re doing, you’ll also want to specify which parts are ‘primary’ and which parts are ‘secondary’, and then pass that into the function as well.
Again, for example:
local button = script.Parent.Parent.TextButton
local button2 = script.Parent.Parent.TextButton2
local button3 = script.Parent.Parent.TextButton3
local selectedColour = script.Parent.Colour.TextLabel
local primaryParts = workspace:WaitForChild("PrimaryPartsFolder")
local secondaryParts = workspace:WaitForChild("SecondaryPartsFolder")
function modifyParts(partsToModify, newColor)
for i, v in pairs(partsToModify:GetDescendants()) do
if v:IsA("BasePart") then
v.Color = newColor
end
end
end
button3.MouseButton1Down:Connect(function()
modifyParts(primaryParts, Color3.fromRGB(table.unpack(selectedColour.Text:split(", "))))
end)
button.MouseButton1Down:Connect(function()
modifyParts(secondaryParts, Color3.fromRGB(table.unpack(selectedColour.Text:split(", "))))
end)
button2.MouseButton1Down:Connect(function()
local lost = workspace:WaitForChild("lost")
local text = lost.SurfaceGui.TextLabel
text.TextColor3 = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
end)
This assumes that button3 and button are the ‘Apply Primary’ and ‘Apply Secondary’ buttons shown, respectively
@reteyetet@ChiefWildin
I have sent you both a friend request, in case you have any further questions you can contact us directly. Or if you need / prefer that we help you directly with the code (TeamCreate)
(I usually respond faster by Roblox chat)
Quick question back to my original question: Where would I put the new copies of the model? I believe I have mentioned before, that this GUI is different for each client.
If you want a different copy of the model for each player, put the original model into ReplicatedStorage, and then clone it into the Camera at the beginning of your script. This creates a client-only version of the model that won’t go to the server.
If you’re just making more of the model that any player can edit, then you’ll just clone it within that same folder that you have already.
That might be confusing, does that make sense to you?
If you want each client to look different, I recommend you look at this tutorial, made by Alvin_Blox, so you can create local parts (different for each client). Since if you copy it to the server, it would be the same for all clients.
It’s basically what ChiefWildin explains, but in a video.