I’m trying to make a module that I can require via a local script and do something like local frame = UIModule:CreateElement("Frame"),
I’ve put the module script into ReplicatedStorage alongside a folder with the frames I will be using. How can I clone the frame and have it in the local player?
Your question is pretty confusing, I think I understand some of it, but not enough to help you.
So, your sample code seems to suggest you’re interested in making a blank UIFrame or possibly one with preset properties without having to set them all manually.
However after you explain that you have the frame(s) there with the module.
The pluralization is throwing me off here because if you do want a module that can create these UI components for you why do you have more than one? When creating a frame is the module supposed to give a random one or would you have to name it?
Please let me break this problem down into what I currently understand, and we can build on it.
TL;DR - I don’t know what you’re asking so here’s my best guess.
local UIModule = {}
function UIModule:CreateElement(Element)
return script.Parent:FindFirstChildWhichIsA(Element):Clone()
end
return UIModule
This script assumes the elements you want to create are premade and are children of the same parent as the module script.
Hi, thank you for the reply. Sorry if I made it confusing. What I’m trying to do, is within the module is a pre-made frame, which already has text etc in it. I want to be able to from a localscript do something like local frame = UIModule:CreateElement("Frame") then have properties for it, like frame.Title = "Title Text" and frame.Position = UDim2.new(0, 0, 0, 0) etc. Kind of similar to how ro-strap works.
I like this solution a lot actually to help keep UI elements consistent across the game. I don’t think you need to be completely constructing your GUIs by using this module, but it would work great for situations where you need to fill a ScrollingFrame at run time or something.
I think @RepValor had a pretty good solution. I would do something similar but change a few things:
local UIModule = {}
UIModule.new = function(elemName)
local locatedElem = script:FindFirstChild(elemName)
if locatedElem then
return locatedElem:Clone()
else
error(string.format("Unknown UIModule preset (%s)", elemName))
end
end
return UIModule
This makes the behavior of the UIModule.new(…) very similar to Instance.new(…). It adds a custom error message to be displayed which would make it more obvious when debugging your code that an incorrect usage of UIModule is the culprit. Also, this way requires that all preset UIElements have unique names. You could also improve this to wait for all the children of the cloned preset UI element by waiting for all children of the original to appear in the clone.