So i’m trying to code something that will save the players data when they collect a coin. However I can’t for the life of me figure out what to set PlayerData
to. Any help is appreciated.
Here’s the function in the module script:
function data.CoinGot(player, coin)
local Datastore = DataStore2("PlayerData3", player)
local PlayerData = Datastore:Get(???)
PlayerData.Coins = PlayerData.Coins + coinvalues[coin]
print(PlayerData.playerd.Coins)
Datastore:Set(PlayerData)
end
PlayerData.playerd.Coins
is where the coin data is stored. It is located in another function, data.LoadData(player)
.
@KindaBadToast
This is dependent on how you first set up a players data in OnPlayerAdded (assuming you did that), Based on your code, it appears that your data is based as a table so use this:
local PlayerData = Datastore:Get({})
If it turns out you do not setup data on PlayerAdded then player data may be nil or an empty table, in which case you should initialize .Coins by doing
PlayerData.Coins = 0
PlayerData.Coins+=coinvalues[coin]
--or similar initializing
I just now read this
PlayerData.playerd.Coins
so you will need to add the .playerd. before accessing .Coins
Datastore 2 is harder to code(thats why it is better
Datastore is eaiser to code
I did infact get the players data ready when they join. I just can’t seem to figure out the correct path to save the data. The data is saved in a table aswell. I’m currently not on my computer however so I can’t test other ways to save it or test code sent.