How could I crate items from a ModuleScript to frames?

Hello!
So, I’m currently working on a crate shop. So basically, I’m wanting to assign different crates to each frame. How could I make this possible? I’m confused about how to call the module to the local script and then assigning items to the frame but making sure that no frame is duplicated. How could I make this possible?

the Example guis contents contain “title”, “price”, “BuyButton” and there’s an IntValue in the “price” textlabel named “Price”

Here’s my code:

--  LOCALSCRIPT in the gui.

-- services

local ReplicatedStorage		= game:GetService('ReplicatedStorage')
local MarketplaceService	= game:GetService('MarketplaceService')
local Players				= game:GetService('Players')

-- constants
local player		= Players.LocalPlayer
local PLAYER_DATA	= ReplicatedStorage:WaitForChild('PlayerData'):WaitForChild(player.Name)
local REMOTES		= ReplicatedStorage:WaitForChild('Remotes')
local MODULES 		= ReplicatedStorage:WaitForChild('Modules')
local ITEMS			= MODULES:WaitForChild('Items')
local EXAMPLE 		= script.Example

local GUI			= script.Parent.Market
local HOLDER 		= GUI.Holder

-- Set up shop
for i = 1,8 do
	local itemFrame = script.Example:Clone()		
		itemFrame.LayoutOrder	= i
		itemFrame.Parent 		= HOLDER
		itemFrame.Name			= I

        --  Stuck on assigning items to the 8 frames.

end
-- ModuleScript

local module = {}

function spawnCrates()
	
	local Crates = {
	
		"Test Crate A", 50;
		"Test Crate B", 150;
		"Test Crate C", 250;
		"Test Crate D", 350;
		"Test Crate E", 450;
		"Test Crate F", 550;
		"Test Crate G", 750;
		"Test Item H", 1000;
	
	}	
	
	
	
end

return module

1 Like

I’m a little bit confused on what you mean by ‘assign different crates to each frame’.

Do you think you could explain what you mean by ‘crates’?

The frames shouldn’t be duplicated as long as you are only adding them once.

Yes, crates is what I mean sorry my brain was all over the place last night. But how do I assign the crates to each frame?

I just realized I didn’t even answer your question properly. So my goal is to have different crates players can open to get a type of currency, I’m trying to grow my scripting knowledge by instead of creating ui for each crate to just have a localscript do it for me. I hope this properly answers your question and hopefully you will be able to help me out.

So I assume what you’re asking is how to add these ‘crate’ frames to the item frames (itemFrame) automatically?

If so, you need to loop through the module script’s table for the set of crates and clone a template frame under your script and edit each dependently.

Make sure that you have a table under module in your ModuleScript that has a list of each crate and its information.

I posted the code for my module script, I’m not sure how to sort through it. Could you help me with that? Or help me figure out an easier way to assign crates to itemframes

Your module script should look something like this:

local Module = {}

Module.Crates = {
    {"Crate1", 50},
    {"Crate2", 150}
}

return Module

And you can loop through the table outside of it like this:

local Module = require(PathToModule)

for Index, Value in pairs(Module.Crates) do
    -- Iterate through each of the crate tables.
    -- Example usage: Index = 1, 2, ... Value = {CrateName, CrateCost}
end

Hope that helps! :slight_smile:

1 Like

Ohh! Thank you! Yes this should help me a lot!

1 Like

I think you can check ForeverHD’s Crate system:

Hello!
It’s been a while but I just got around to doing this, I’m working on using the same method that you used, but with vehicle names. Here’s what the table looks like:

ModuleScript

local module = {}	

	module.Crates = {	
		"Test Crate A", 50;
		"Test Crate B", 150;
		"Test Crate C", 250;
		"Test Crate D", 350;
		"Test Crate E", 450;
		"Test Crate F", 550;
		"Test Crate G", 750;
		"Test Item H", 1000;	
	}
	
	module.Vehicles = {
	
		"Basic Truck", 5000;
		"Delivery Truck", 10000
	
	}

return module

Here’s my script for assigning the vehicle names and prices to the gui local script

-- services

local ReplicatedStorage		= game:GetService('ReplicatedStorage')
local MarketplaceService	= game:GetService('MarketplaceService')
local Players				= game:GetService('Players')

-- constants
local player		= Players.LocalPlayer
local PLAYER_DATA	= ReplicatedStorage:WaitForChild('PlayerData'):WaitForChild(player.Name)
local REMOTES		= ReplicatedStorage:WaitForChild('Remotes')
local MODULES 		= ReplicatedStorage:WaitForChild('Modules')
local ITEMS			= MODULES:WaitForChild('Items')
local EXAMPLE 		= script.Example

local GUI			= script.Parent.Parent.VehicleStore
local HOLDER 		= GUI.Holder

-- Setup vehicle frame

local Module = require(ITEMS)
	for Index, Value in pairs(Module.Vehicles) do
		local itemFrame = script.Example:Clone()		
		itemFrame.LayoutOrder	= Index
		itemFrame.Parent 		= HOLDER
		itemFrame.Name			= Index
		itemFrame.Type.Text 	= Value
		itemFrame.Price.Text	= Value
end

Here’s what the outcome looks like:

Vehicle Frame

So, what I’m wanting it to look like is Dark text is the name, and then the green text is the price. How can I fix this?

Glad you got it working! :slight_smile:

Your first problem here is that you’re formatting the vehicles under your ModuleScript in the wrong way for what your use case is.

You are setting Basic Truck as a value in the table, but you’re also setting 5000 as a separate value. There is currently no way to identify which cost is for which type of vehicle!

You need to either combine both of these values into a table like one of these values.

  1. {"Basic Truck", 5000}
  2. {Name = "Basic Truck", Cost = 5000}
  3. ["Basic Truck"] = 5000.

After setting it to the first value for the example you can loop through it like this:

for Index, Value in pairs(Module.Vehicles) do
    local itemFrame = script.Example:Clone()
    local Type = Value[1] -- Or Value.Name for the second example.
    local Cost = Value[2] -- Or Value.Cost for the second example.
    -- Create the itemFrame and use the Cost value for the Price label.
end
1 Like

Ohh! I understand now, thank you so much!

1 Like

Hello again!
I’ve been working on another script using this method, how would I only get 3 values in the Module? Would I use, would I use a math.random for this?

If you only want to grab and show 3 of the crates you would use a variable as a limiter.

Example usage:

local TimesRan = 0

while true do
    if TimesRan >= 5 then
        break
    end
    TimesRan = TimesRan + 1
end

math.random works like Random.new(), generating a pseudo-random number within the numbers you give the functions.

Example usage:

print(math.random()) -- Prints a random number from 0-1
print(math.random(1, 5)) -- Prints either 1, 2, 3, 4, or 5.
print(Random.new():NextInteger(1, 5)) -- Works like above
print(Random.new():NextNumber(1, 5)) -- Works like above too except the decimals are randomized.

*I would rather use Random.new() over math.random() since it allows you to easily generate either an integer or a double, but that’s up to your preference since that might be the only difference.

Hello!
Sorry for the late reply, unfortunately, I do not understand exactly what you’re saying for assigning 3 the 3 crates.

Here’s my code for assigning them!

local Module = require(ITEMS)
	for Index, Value in pairs(Module.SellInfo) do

	local itemFrame = script.OfferX:Clone()		
		itemFrame.LayoutOrder				= Value.Cost
		itemFrame.Parent 					= HOLDER
		itemFrame.Name						= Value.Name
		itemFrame.Info.Title.Text 			= itemFrame.Name
		itemFrame.Info.Crates.Text 			= Value.Amount.. " Crates"
		itemFrame.Info.Sell.TextLabel.Text	= "SELL FOR $"..addComas(tostring(Value.Cost))

		itemFrame.Info.Sell.MouseButton1Click:Connect(function()
			SOUNDS.Click:Play()
			if WAREHOUSE.Vehicle.Value ~= "" and WAREHOUSE.Crates.Value >= 3  then-- Check to see if they own a vehicle, check to see if they have more than 3 crates
				REMOTES.SellCrates:FireServer("SellCrates", tonumber(Value.Amount), tonumber(Value.Cost))
		
			else

				print("player cannot sell:(")
			

		end
	end)	
end

Thank you for all your help in advance!

I don’t understand if “how would I only get 3 values in the Module” means how you would get 3 values from the module and only show 3 or if you mean to grab 3 random values from your module.

A mix of both. How would I get 3 random values and also only show those 3