Table's table.insert breaks after one use? [SOLVED]
I have a player join, upon joining a history table is created to save their purchases, their history table is updated with their previous purchases transferring them from the datastore by using unpack(), if they want to buy something the click the GUI which sends an Event signal to tell the script to calculate if they can buy the item, if not it returns a print("[System]: Already bought.") However, upon joining the datastore fires an event which sets the history table to the users previous purchases, using table.insert, if I don't do this the player can re-buy what they already have. However, when I try to use table.insert when the player buys something else, it doesn't insert into the table, I have no idea why, why apparently whenever I unpack the datastore key as a table and send it to be the history table, then try to insert something it doesn't work.
02 | moneye.Event:connect( function (cost, plr, id) |
03 | if cost < = playerLeaderstats [ plr ] [ "Cash" ] then |
04 | local user = "User_" .. plr.userId |
05 | local saves = Characters:GetAsync(user) |
06 | local chardata = saves |
07 | print (id, "id should be " .. chardata ) |
08 | if id = = chardata then |
10 | return print ( "[System]: Already bought." ) |
12 | playerLeaderstats [ plr ] [ "Cash" ] = playerLeaderstats [ plr ] [ "Cash" ] - cost |
13 | cashe:Fire(playerLeaderstats [ plr ] [ "Cash" ] ) |
14 | print ( unpack (history [ player ] ), "b4 insert" ) |
15 | table.insert(history [ player ] , id) |
16 | print ( unpack (history [ player ] ), "after insert" ) |
17 | Characters:SetAsync(user, unpack (history [ player ] )) |
18 | print (saves, "after setasync" ) |
27 | historye.Event:connect( function (data, plr) |
28 | if plr.userId = = player.userId then |
31 | print ( unpack (history [ plr ] ), "before" ) |
32 | local insertthis = data |
33 | table.insert(history [ plr ] , insertthis) |
34 | print ( unpack (history [ plr ] ), "after" ) |
Any clarifications or code needed I can provide it.
EDIT: Solved by re-writing the code in a different way, perseverance will usually get the job done!