Earlier today, I used a guide to make a simple datastore, storing the value of cash. Not only does the code not give any sort of error message after not saving any of the data, after the SetAsync, attempting to print the data variable doesn't work altogether. Is there anyway I can fix this?
(loading code, works fine, just in case though)
01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local myDataStore = DataStoreService:GetDataStore( "myDataStore" ) |
03 |
04 | game.Players.PlayerAdded:Connect( function (player) |
05 |
06 | local leaderstats = Instance.new( "Folder" ) |
07 | leaderstats.Name = "leaderstats" |
08 | leaderstats.Parent = player |
09 |
10 | local Cash = Instance.new( "IntValue" ) |
11 | Cash.Name = "Cash" |
12 | Cash.Parent = leaderstats |
13 |
14 | local playerUserId = "Player_" ..player.UserId |
15 |
(Saving Code(Where the problem likely is))
01 | game.Players.PlayerRemoving:Connect( function (player) |
02 | local playerUserId = "Player_" ..player.UserId |
03 |
04 |
05 |
06 | local data = player.leaderstats.Cash.Value |
07 | print (data) |
08 | local success, errormessage = pcall ( function () |
09 | myDataStore:SetAsync(playerUserId, data) |
10 | end ) |
11 | print (data) |
12 | if success then |
13 | print ( "Data successfully saved!" ) |
14 | else |
15 | print ( "There was an error!" ) |
Thanks in advance!
Try using :BindToClose
. If you're the only one in the server and you leave, the server may shut down first and not allow all your functions to finish running. The server can also shut down unexpectedly even if there is more than 1 player. :BindToClose
will allow bound functions to run 30 seconds before the server shuts down. You can just add this at the end of your script.
01 | game:BindToClose( function () |
02 | for i, player in pairs (game.Players:GetPlayers()) do -- looping through every player allows this function to run for all the players left in the server |
03 | local playerUserId = "Player_" ..player.UserId |
04 |
05 |
06 |
07 | local data = player.leaderstats.Cash.Value |
08 | print (data) |
09 | local success, errormessage = pcall ( function () |
10 | myDataStore:SetAsync(playerUserId, data) |
11 | end ) |
12 | print (data) |
13 | if success then |
14 | print ( "Data successfully saved!" ) |
15 | else |
16 | print ( "There was an error!" ) |
17 | warn(errormessage) |
18 | end |
19 | end |
20 | end ) |
Hey! I had the same problem, but this script works. Make sure to put it in server script service as a script. (Click view source and copy all of it there.)
01 | local Players = game:GetService( "Players" ); |
02 |
03 | local DataStoreService = game:GetService( "DataStoreService" ); |
04 |
05 | local Datastore = DataStoreService:GetDataStore( "MBucksDataStore" ); --Can be changed, but if you are using this script and players have already have data, don't change it or else it will wipe ALL data. |
06 |
07 |
08 | Players.PlayerAdded:Connect( function (player) |
09 | local leaderstats = Instance.new( "Folder" , player); |
10 | leaderstats.Name = "playerstats" ; --If you want it to show up on the leaderboard, name it "leaderstats". |
11 |
12 | local cash = Instance.new( "IntValue" , leaderstats); |
13 | cash.Name = "MBucks" ; --This can be changed to whatever your currency name is. |
14 |
15 | local data = nil ; |