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

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Cash = Instance.new("IntValue")
    Cash.Name = "Cash"
    Cash.Parent = leaderstats

    local playerUserId = "Player_"..player.UserId

    -- Load Data

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)
    end)

    print(data)

    if success then
        Cash.Value = data
        -- Set our data to the player's values
    end


end)


(Saving Code(Where the problem likely is))


game.Players.PlayerRemoving:Connect(function(player) local playerUserId = "Player_"..player.UserId local data = player.leaderstats.Cash.Value print(data) local success, errormessage = pcall(function() myDataStore:SetAsync(playerUserId, data) end) print(data) if success then print("Data successfully saved!") else print("There was an error!") warn(errormessage) end end)

Thanks in advance!

0
What does it output? User#30567 0 — 3y
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 — 3y

2 answers

Log in to vote
0
Answered by 3 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.

game:BindToClose(function()
    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
        local playerUserId = "Player_"..player.UserId



        local data = player.leaderstats.Cash.Value
        print(data)
        local success, errormessage = pcall(function()
                myDataStore:SetAsync(playerUserId, data)
            end)
        print(data)
        if success then
            print("Data successfully saved!")
        else
                print("There was an error!")
                warn(errormessage)
        end
    end
end)
0
worked like a charm! :D patty323 22 — 3y
Ad
Log in to vote
0
Answered by 3 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.)

local Players = game:GetService("Players"); 

local DataStoreService = game:GetService("DataStoreService");

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.


Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player);
    leaderstats.Name  = "playerstats"; --If you want it to show up on the leaderboard, name it "leaderstats".

    local cash = Instance.new("IntValue", leaderstats);
    cash.Name =  "MBucks"; --This can be changed to whatever your currency name is.

    local data = nil;

    local s, e = pcall(function()
        data = Datastore:GetAsync(player.UserId.."-cash")
    end)

    if(s) then
        print("Data successfully loaded.")
        data = data or 0 -- If no data has been saved, it replaces it with 0 for default.
        cash.Value  = data
    else
        warn("There was a problem loading data:", e);
    end
end);


Players.PlayerRemoving:Connect(function(player)
    local leaderstats = player.playerstats --Use the name of your folder name. (Refer to line 12.)
    local cash = leaderstats.MBucks --This must be the name of your currency. (Refer to line 13.)

    local s, e = pcall(function()
        Datastore:SetAsync(player.UserId.."-cash", cash.Value)
    end);

    if(s) then
        print("Data successfully saved.");
    else
        warn("There was a problem saving data:", e);
    end
end)

Answer this question