Duplicate models that have functions in StarterGUI?

I have a model that I want to make more copies of. But the model gets its functions and features from StarterGUI, and the scripts in StarterGUI state for example:

local part7 = workspace:WaitForChild("p3")

and so on. Does anyone have a solution or alternative to this?
To put it simply I have a model that has functions in StarterGUI, but those functions are linked to that one model from this

 local lost = workspace:WaitForChild("lost")
    local part2 = workspace:WaitForChild("lost")
	local part3 = workspace:WaitForChild("click")
	local part4 = workspace:WaitForChild("Partee")
	local part5 = workspace:WaitForChild("p1")
	local part6 = workspace:WaitForChild("p2")

If I want to make more copies of that model, with the same functions what do I do?

This is not a very efficient way of doing this. You will need to share more information to help us answer in the best way possible: Sharing more code and screenshots could help.
I recommend grouping all of these and then changing the script to reference them like this

local model = game.Workspace.Model
local part7 = model["p3"] 

From here you could duplicate both the model, and the script and just change the names.

This post is just as confusing as your last one.

If I’m reading this right, you have a model in workspace (called “p3”?) that you want to clone, but there are functions in a script in StarterGui (and by extension, each PlayerGui) that use that model that would need to be duplicated as well for the cloned model. Am I correct with all of that?

The simple way to do this is to rename the variable’s to different names. But I want to know if there is another way to do this easier? I have to do this about 55 times so I am in desperate need.

Ok, can you elaborate more on that? Its possibly the exact answer I need. Here is the whole script.

local button = script.Parent.Parent.TextButton
local button2 = script.Parent.Parent.TextButton2
local button3 = script.Parent.Parent.TextButton3
local selectedColour = script.Parent.Colour.TextLabel

button3.MouseButton1Down:Connect(function()
    local lost = workspace:WaitForChild("lost")
    local part2 = workspace:WaitForChild("lost")
	local part3 = workspace:WaitForChild("click")
	local part4 = workspace:WaitForChild("Partee")
	local part5 = workspace:WaitForChild("p1")
	local part6 = workspace:WaitForChild("p2")
	local part7 = workspace:WaitForChild("p3")
	local part8 = workspace:WaitForChild("p4")
	local part9 = workspace:WaitForChild("p5")
    local part10 = workspace:WaitForChild("b1")
	local part11 = workspace:WaitForChild("b2")
	local part12 = workspace:WaitForChild("b3")
	local part13 = workspace:WaitForChild("b4")
	local part14 = workspace:WaitForChild("b5")
	local part15 = workspace:WaitForChild("b6")
	
    lost.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
    part2.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part3.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part4.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part5.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part6.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part7.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part8.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part9.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part10.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part11.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part12.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part13.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part14.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
	part15.Color = 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)

Yes I know its terrible, I need to know how to fix this.

Is this code in a LocalScript within StarterGui?
If so, I don’t think you can change the color of a part in the workspace from the client.

Yo, this is simple.

Put all the parts that you need to modify into a Model or a Folder in workspace. Then, iterate through the Model or Folder and do what you need to do for each one. In your case, it’d look something like this:

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")

button3.MouseButton1Down:Connect(function()
    for i, v in pairs(partsToModify:GetChildren()) do
        v.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
    end
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)

Where “partsToModify” is the Folder that contains all the parts that you’re talking about duplicating.

Does that make sense?

1 Like

Yes, I need all models to have the same function but can run through different Clients.

You should not add a :GetChildren or :GetDescendants in the variable partsToModify?

You have already fixed it.

1 Like

You’re completely right, I’ve edited the code to reflect that. It was my oversight, thanks for catching it.

1 Like

Is it also possible to add a Model instead of individual parts in the PartsFolder?

Yes, but you’ll handle each one differently. For example:

for i, v in pairs(partsToModify:GetChildren()) do
    if v:IsA("BasePart") then
        v.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
    elseif v:IsA("Model") then
        for index, child in pairs(v:GetChildren()) do
            if child:IsA("BasePart") then
                child.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
            end
        end
    end
end
2 Likes

In this case I think you should use :GetDescendants()
(Sorry if I’m wrong.)
If I remember correctly :GetChildren() only returns the first things it finds.
Instead :GetDescendants() returns EVERYTHING he finds wherever you indicate.

Check these articles for a better explanation of both.
:GetChildren()
:GetDescendants()

1 Like

You’re right, you could also use :GetDescendants(), it just depends on how deep into the children you wish to go. Using :GetDescendants(), the solution would be

for i, v in pairs(partsToModify:GetDescendants()) do
    if v:IsA("BasePart") then
        v.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
    end
end

And it will change any and every BasePart-derived object it finds within partsToModify. Thanks for the suggestion @nanitook

2 Likes

No problem, that’s what we’re all for. To help each other. :slight_smile:

1 Like

This is how my code is looking so far. Is there any potential errors?

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")

button3.MouseButton1Down:Connect(function()
    for i, v in pairs(partsToModify:GetChildren()) do
    if v:IsA("BasePart") then
        v.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
    elseif v:IsA("Model") then
        for index, child in pairs(v:GetChildren()) do
            if child:IsA("BasePart") then
                child.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
            end
        end
    end
end
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)

I don’t see any errors with the code. Your indentation’s slightly off, but that won’t affect functionality. Just know that this won’t account for models within models that you place in partsToModify. To reach all levels deep within the Folder, use the :GetDescendants() that I posted, which was suggested by nanitook.

If I’m reading it right, ‘Colour’ more than likely refers to the name of some sort of GuiObject, not a property.

Yes, now reading it better I am realizing the same. script.Parent.Colour.TextLabel doesn’t make much sense if you want to change the color.
Sorry about that.

1 Like

Yes this is for a Colour wheel. Here is the updated code:

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")

button3.MouseButton1Down:Connect(function()
    for i, v in pairs(partsToModify:GetDescendants()) do
    if v:IsA("BasePart") then
        v.Color = Color3.fromRGB(table.unpack(selectedColour.Text:split(", ")))
    end
end
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)