Referencing data in module scripts from GUI

Hello,

Currently, I am trying to store player’s units/towers in a module script:

export type Inventory = {
	Currency: Currency,
	Items: Items,
	Units: {Unit}
}

export type Currency = {
	Coins: number
}

export type Items = {
	Candies: number
}

export type Unit = {
	OriginalOwnerID: number,
	Name: string,
	Level: number,
}

local module = {}

local Inventories = {}

In the inventory GUI, a frame will be created for every unit the player has collected, but I am having difficulty thinking of a way for me to map these frames to the units stored inside the module script, is there a better way than assigning a GUID to every unit and then mapping them to each frame by placing the GUID in the frame attribute? Will assigning a GUID for every unit be too wasteful? Considering I will need to store these in the datastore. Thanks in advance!

1 Like

would a normal for loop work here? loop over the inventory and then loop over each unit and create a frame inside it, and give it the name of the item

if there can be duplicate items then you may want to store a unique identifier

yea I was thinking of looping through the inventory and creating a GUID for each unit that is used only when the player is in game and mapping that to the GUI frame, but that seems like extra work compared to just storing a GUID in every unit in the datastore. I am most likely going to just store a GUID inside every unit, just curious what is the norm for handling such issues

EDIT:

export type Inventory = {
	Currency: Currency,
	Items: Items,
	Units: {[string]: Unit}
}

for now I am tracking the inventory like this, where the key ([string]) is the GUID, if anyone has a better idea please do tell!

2 Likes