#Getting item names from varying amount of items

1 messages · Page 1 of 1 (latest)

solid niche
#

I am trying to get the names from items for a little ordering project, but there could be different amounts of items and I don't know how to get the names of them if there are different amounts each time. If this is confusing sorry, just ask me to elaborate or try to help explain more lmao.

coarse minnow
#

each time you add a new item you should have a table that keeps track of it with the item name as key and its count and then maybe reference that table

#

like what do you have right now that keeps track of what items are in the cart because if you dont have that then there is no way you can get the item names

solid niche
#

but how would I add and removes items from a table?

#

Isn't a table written code?

coarse minnow
#

table.insert() and table.remove() are methods that you can use to add and remove elements from a table

solid niche
#

OH?

coarse minnow
#

i suggest you look at the documentation to learn more about tables as they are pretty much the only data structure in roblox

#

and are essential

solid niche
#

ahh alright, thanks! this was supposed to be an easy project, guess not 💀 😭

#

okay so, this is the script I put in each item to get added to the cart, @coarse minnow. is there a way to access a table that's in a seperate script? just like if I wanted to put a table.insert() in this script but have the table be in another? Because I don't think I can make it work if I have the table here. Sorry if this is confusing

coarse minnow
#

you would have to use a module script

#

so that multiple scripts can access it

solid niche
#

hmmm

#

alright

solid niche
coarse minnow
#

yes

#

as long as it returns a table

#

this is the format for modulescripts

local module = {}

return module

solid niche
#

sweet, thanks. I'll just look into module scripts a little bit to figure out how to use em

coarse minnow
#

and to get them you do this
local module = require(PathToModuleScript)

coarse minnow
#

and then the module variable would hold a reference to that table

#

yes

solid niche
#

thanks!

solid niche
digital vaporBOT
#

studio** You are now Level 5! **studio

coarse minnow
#

replicatedstorage

solid niche
coarse minnow
#

i guess but it might be hard to find it

solid niche
#

okie dokie, thanks!

coarse minnow
#

like if you just put it in replicated storage its easier

solid niche
#

kk

coarse minnow
#

you just need to do require(game:GetService("ReplicatedStorage").ModuleName)

solid niche
#

you have to getservice replicated storage?

#

BAHAH HWHAT?

#

I just do game.replicatedstorage.xxxx

#

or is it cus it's a module script

coarse minnow
#

no it shouldnt make a difference

solid niche
#

oh, alright!

coarse minnow
solid niche
#

I'll take a look. Cus the only thing I ever had to do getservice for is tweening and what not

timid citrus
#

you can use the instance tree to build the table, like loop through all of Menu.Background.Order.Cart :Children and insert Item.Text for the name, or give each item an attribute if the item's key name is different to its display name, and with this table you can fire it to your remote event. very simple really. there's always more than 1 way to solve any given problem Thumbs

timid citrus
coarse minnow
#

yea that would be better mb

coarse minnow
#

but you would have to parent those items added to the cart to that cart instance so get children actually gets the items in the cart

solid niche
#

I should maybe research and learn a little more lmao

timid citrus
# solid niche I just do game.replicatedstorage.xxxx

game:getservice is preferred method for all roblox services, it's mostly for preference and consistency, but for example if you rename ReplicatedStorage (yes you can rename them), then game.ReplicatedStorage won't work, but game:GetService("ReplicatedStorage") will.

#

a bit like you can do local Players=game.Players but it is my preference to call this local PlayerService=game:GetService("Players") so the 'Players' keyword isn't consumed by the service

#

no functional difference really, just preference.

solid niche
#

@coarse minnow what'd i do wrong?

timid citrus
solid niche
#

i've never used tables sooo

#

I don't know ANYTHING

solid niche
coarse minnow
#

no dont use what i did @timid citrus 's suggestion was better

solid niche
#

scold me all I want

solid niche
coarse minnow
#

just parent the items added to the cart to the cart ui

#

then get those items by doing the get children method on the cart ui

timid citrus
solid niche
#

so not on the client side

timid citrus
#

yeah get the information on the client then send it to the server ?

solid niche
#

well on the client side, just for everyone

coarse minnow
#

so like when you add snickers to the cart, is there a new text label being created with the text snickers that is being parented to the cart ui?

solid niche
#

yup!

#

well

#

I can make it do that

coarse minnow
#

then use get children from the cart ui to get back the names of each food item

solid niche
#

right now its 'Item#'..cartAmount.value

solid niche
coarse minnow
#

no because get children returns a table

solid niche
#

ohh okay

coarse minnow
#

of all the children

timid citrus
#

luau is all about working with tables

solid niche
#

so even if the amount of items is changing, it's still going to get all of them?

solid niche
coarse minnow
#

yes because if you add 3 snickers then there should be 3 text labels with the snickers text under the cart gui

timid citrus
coarse minnow
#

in fact you dont even really need to use an int value to keep track of how many items are in the cart

#

you could just get the length of the table returned by get children

solid niche
timid citrus
coarse minnow
#

you can use this operator to get the length: #

timid citrus
#

because that's true - it's closer to 95% which gets thrown out fingerguns

solid niche
#

probably doing something wrong @coarse minnow

#

it's not printing it

coarse minnow
#

where do you parent the items

solid niche
#

the cart

coarse minnow
#

when you add them to the cart

solid niche
#

Ill show you

#

well, it's printing all the data, and the cartItems are in the data

#

so the items go inside the cart

#

if any of this is confusing lmk

coarse minnow
#

idk why get children is returning an empty table

#

that would mean it has no children

#

maybe you can invite me to team create and i can see

timid citrus
coarse minnow
#

hes printing on the server but its an empty table

timid citrus
#

oh i see yea they're passing the instances directly which only exist on client

#

need to pass the names

solid niche
#

ohh

timid citrus
#

getchildren gets you the instances, you need to do the next step and build a table of all the names from those instances

#

which is just a trivial for loop

coarse minnow
#

cant you also access the player's cart ui through player gui on the server?

solid niche
#

not through the server

timid citrus
#

you can if you created it on the server, but that is not the case here

solid niche
#

BYE THAT'S NOT GOOD

#

I could try to parent them to a folder inside the cart instead

coarse minnow
#

yea that would be better

solid niche
#

alr

#

wait no

#

I can't do that

#

cus then it takes away all of the other stuff

#

wait no

#

not that

#

it takes away the ui list layout

coarse minnow
#

then create a separate frame with only the ui list layout and the food items

solid niche
#

hmm okie dokie

#

or I could just put it in a folder with the layout

#

that's what I did

timid citrus
solid niche
#

I think maybe

#

I do get children

#

and then I do something like if it is a frame then put it on a table yeah?

timid citrus
#

yes

#

its called filtering

solid niche
#

if I can spell 💀

timid citrus
#

so filter the stuff in the table for what you want, most likely is frames

solid niche
#

okay, but I can't do something like this

#

so what should I do instead

#

OHH

#

should it be in an if statement or smth?

coarse minnow
#

dont do that

solid niche
#

I know

coarse minnow
#

just do cart:getchildren

solid niche
#

it won't work

coarse minnow
#

then when you iterate through it

solid niche
coarse minnow
#

check if its a frame

#

like this:

local itemNames = {}

for i, v in cartItems do
    if v:IsA("Frame") then
        table.insert(itemNames, v.Name) 
    end
end
solid niche
#

kk

digital vaporBOT
#

studio** You are now Level 6! **studio

solid niche
#

well

#

how would I put that in here

coarse minnow
#

like just modify it to get the name

solid niche
#

sorry that I'm confused

#

where would I put that script?

coarse minnow
#

in the mouse button 1 click event

solid niche
#

ahh okay okay

coarse minnow
#

so what it does is it goes through the cartItems table, where v is the child of the cart ui, and then it checks whether or not v is a frame, if it is then it appends (adds to the end of the table) the name of v to a table called itemNames

solid niche
#

I don't think I organized it correctly lmao, it wouldn't print the itemnames @coarse minnow

coarse minnow
#

that means you dont have any children in cartItems that are frames

digital vaporBOT
#

studio** You are now Level 8! **studio

coarse minnow
#

when you add an item to the cart what kind of instance is it?

solid niche
#

OHH WAIT

#

I KNOW WHAT IT IS

coarse minnow
#

is it a text label or frame

solid niche
#

HOLD ON

#

AHAHAHA

#

WOOPSIES

#

It's cus I made a folder

#

but now I don't have to anymore

#

🤩

#

YOOO IT'S WORKING

#

okay so

#

now

#

omg now what

#

idk 😭

#

@timid citrus @coarse minnow I want to say THANK YOU SO MUCH. I could not have done this with out you and sorry you had to handle my stupidity lmao. Thank you so so much.