Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is my DataStore not saving data, nor giving any sort of error?

Asked by 4 years ago

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)

01local DataStoreService = game:GetService("DataStoreService")
02local myDataStore = DataStoreService:GetDataStore("myDataStore")
03 
04game.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 
View all 31 lines...

(Saving Code(Where the problem likely is))

01game.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!")
View all 21 lines...

Thanks in advance!

0
What does it output? User#30567 0 — 4y
0
All it outputs are the several print(data)'s that I put around the code for debug purposes, but no specific error messages. Also, It doesn't print out the one after the SetAsync. patty323 22 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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.

01game: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
20end)
0
worked like a charm! :D patty323 22 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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.)

01local Players = game:GetService("Players");
02 
03local DataStoreService = game:GetService("DataStoreService");
04 
05local 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 
08Players.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;
View all 44 lines...

Answer this question