How do I use a ModuleScript to return a table

I have a LocalScript called UIHandler and a ModuleScript called UISettings in the same folder.

I want to create an ImageButton and ImageLabel using the settings in UISettings. The script works when I just add the data for the ImageButton and ImageLabel to the LocalScript but not when I call it from the ModuleScript.

– Local Script

local util = require(script.Parent:WaitForChild(“Utilities”))
local UISettings = require(script.Parent:WaitForChild(“UISettings”))

function CreateMobileChat()
local test = util.Create(“ImageButton”)({
UISettings.MobileHideChatIconButton
})
local test2 = util.Create(“ImageLabel”)({
UISettings.MobileHideChatIconImage
})
test2.Parent = test
return test
end

CreateMobileChat().Parent = game.Players.LocalPlayer.PlayerGui.MainGUI.TopBar

– ModuleScript

local module = {}

local util = require(script.Parent:WaitForChild(“Utilities”))

module.Chat = {
}

module.MobileChat = {
}

module.MobileHideChatIconButton = {
Name = “ChatVisible”,
Size = UDim2.new(0, 50, 0, 36),
Image = “”,
AutoButtonColor = false,
BackgroundTransparency = 1,
}

module.MobileHideChatIconImage = {
Name = “ChatVisibleIcon”,
Size = UDim2.new(0, 28, 0, 27),
Position = UDim2.new(0.5, -14, 0.5, -13),
BackgroundTransparency = 1,
Image = “rbxasset://textures/ui/Chat/ToggleChat.png”,
}

return module

– Utility Function I’m using if that helps

function util.Create(instanceType)
return function(data)
local obj = Instance.new(instanceType)
for k, v in pairs(data) do
if type(k) == ‘number’ then
v.Parent = obj
else
obj[k] = v
end
end
return obj
end
end

Now it creates the ImageButton perfectly fine if I do this:
local test = util.Create(“ImageButton”)({
Name = “ChatVisible”,
Size = UDim2.new(0, 50, 0, 36),
Image = “”,
AutoButtonColor = false,
BackgroundTransparency = 1,
})

I want it to do the same thing but using this line of code:
local test = util.Create(“ImageButton”)({
UISettings.MobileHideChatIconButton
})

It doesn’t return the table the way I want it to though.

1 Like

Just a tip for the future, make sure to format your code properly on the forum using like so:
```
– Insert code here
```

Your problem here is that util.Create is expecting a table containing your keys and values. You provide a table value, holding the UISettings.MobileHideChatIconButton table.

To avoid passing that table inside of another table, simply remove the curly braces:

local test = util.Create("ImageButton")(
    UISettings.MobileHideChatIconButton
)
2 Likes