#Tables comes out empty for some reason

1 messages · Page 1 of 1 (latest)

zinc hawk
#

Hello yall, im trying to make when a player joins the server, the server will get their userid and then insert the userid as a key (therefore the playerID is the key), and through that you will be able to access the Stats value that are assigned to the UserId of the player on PlayerData,

Summary I have ConstantsVariable of Data on my module, the gamecore fetches the table values on the module and assigned to the player userid on a table

but for some reason, the table always comes out empty, I tried everything and for some reason it doesnt work, it doesnt let me store the playername outside of the PlayerAdded:Connect section

swift gust
#

you need to assign player data inside the PlayerAdded event using their userid as the key; if you try storing or accessing it outside that event, the table will be empty because the player hasn’t joined yet.

zinc hawk
swift gust
#

store player data in a global or module-level table inside PlayerAdded, then access that table anywhere after the player joins—no need to put all code inside PlayerAdded. just make sure you only use the data once it’s set.

errant magnet
#

why aren't you using datastore?

errant magnet
#

that's probably what you intended to do

zinc hawk
cedar oarBOT
#

studio** You are now Level 3! **studio

zinc hawk
# errant magnet your `PlayerData` table on the server, and doing `PlayerData.hi=5` or `PlayerDat...

No no, the reason why im not using data store is basically for testing, and also I just want to store the player data on the server temporarily, where its easier to manage and access, the “hi” was just a test and to show what was going on, I intended to save the player data on data store later on(when they leave or in increaments of regular saving every 5 minutes), the table on the server is temporary, think it as RAM memory that later will be stored into a SSD

errant magnet
#

do bear in mind you've got that table as a local variable so unless you pass it out of that script somehow you wont be able to access it anywhere else, use a modulescript like beefiboi said or put it in _G

zinc hawk
errant magnet
#

or, turn it into a bunch of instancevalues like numbervalue and boolvalue and just read from them when you're ready to save. this is a very stable and reliable technique and also replicates the data to the client without any extra work.

zinc hawk
errant magnet
#

then you just do datatable=require(sharedtabledata) datatable[player.userid] etc

zinc hawk
errant magnet
zinc hawk
errant magnet
#

practice makes perfect fingerguns

zinc hawk
errant magnet
#

the only caveat to the instancevalue method is that sometimes you want things not to replicate, so you have to set up a system where some things go into the player, and some things go into some folder in serverstorage

#

or you might want more complex data like a table, which you can absolutely still do with instances it's just a bit harder since you need to watch for datafolder.childadded/childremoving

#

like inventory for example

#

you might even combine the instancevalues with a server-sided table. there are no incorrect answers Thumbs

zinc hawk
#

Hmmmm an inventory system like that aint bad, i will see what i do tmrw morning

#

Thank you so much by the way

#

And also i heard somebody saying instancevalues are a bit bad?, they told me to use leaderstats or attributes for some reason

errant magnet
#

roblox can handle hundreds of thousands to millions of instances no problem.

zinc hawk
#

Ohh there is no worries then

errant magnet
#

just dont do game:getdescendants() if you've got that many hehe

zinc hawk
#

Get:descrndants? thats a new one

#

Isnt getchildren

#

The same?

#

Or is there a different property?

zinc hawk
#

Alright guys, I followed what yall said, and it worked, thank you so much once again, just one thing, I used task.clone(), in order to copy the data on constants, since it was making a new array and putting inside that array the values for some reason, and I also saw online that doing that with tables makes the variable into a pointer to the table, so the values would be changed in both tables, which was not ideal, but still, it all worked out in the end