I'm trying to save a player's data, but it does not appear to save. It gives no errors, and also used to work. It just randomly stopped working
01 | local Data = game:GetService( "DataStoreService" ):GetDataStore( "Cash" ) |
02 | game.Players.PlayerAdded:Connect( function (plr) |
03 | local Folder = Instance.new( "Folder" ,game.ServerStorage) |
04 | Folder.Name = plr.Name |
05 | local Cash = Instance.new( "IntValue" ,Folder) |
06 | Cash.Name = "Cash" |
07 | local Levels = Instance.new( "IntValue" ,Folder) |
08 | Levels.Name = "Levels" |
09 | local SavedItems = Data:GetAsync(plr.userId) |
10 | wait(. 1 ) |
11 | if SavedItems then |
12 | Cash.Value = SavedItems.Cash or 0 |
13 | Levels.Value = SavedItems.Levels or 0 |
14 | else |
15 | Cash.Value = 0 |
This is probably because the player's UserId is a 64-bit integer. GlobalDataStore:SetAsync() requires a string key.
You can convert the UserId to a string using the tostring() function, like this:
1 | Data:SetAsync( tostring (plr.UserId), Saving) |
Or, you can concatenate the UserId to a string, like this:
1 | Data:SetAsync( "user_" ..plr.UserId, Saving) |
#1 is untested. #2 is tested.