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

leaderstats not saving need help?

Asked by 3 years ago

I am using 2 scripts 1 to create the IntValue and 1 to save it but its not saving in roblox studio and outside roblox studio (game itself)

Data script

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Money"
    money.Value = 0
end)

Save Script:

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(player)
    wait()
    local playerkey = "id_"..player.UserId
    local saves = player.leaderstats.Money
    local save = ds:GetAsync(playerkey)

    if save then
        saves.Value = save[1]
    else 
        local numbersave = {saves.Value}
        ds:GetAsync(playerkey, numbersave)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    ds:GetAsync("id_"..player.UserId, {player.leaderstats.Money.Value})
end)

The script i use to increase the money value of the player:

local payCheckTime = 25

while true do
    wait(payCheckTime)
    Plr.leaderstats.Money.Value = Plr.leaderstats.Money.Value + 25
end

3 answers

Log in to vote
1
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Before getting into the actual problem, why do you have two different scripts for creating leaderstats and loading and saving them? These all could be in one script to make things easier. Also, you shouldn't really try solving issues by adding an unnecessary wait() at the beginning -- WaitForChild exists.

Going into the actual problem now, there are a few reasons as to why your data doesn't save:

  • In studio specifically, you may not have Enable Studio Access to API Services toggled on. If not, go into Home > Game Settings > Options > Enable Studio Access to API Services
  • If you're the only player in the server and have left the game, the server is not given enough time to save the data and the server shuts down abruptly.
  • Data stores use Web API calls, which could potentially fail at times due to internal server errors

So, how can the two issues below be solved? First, you would need to implement a BindToClose callback. Whenever the server is scheduled to shut down, BindToClose will make the server wait a maximum of 30 seconds before the server can shut down and execute all callbacks delegated to the function. This is where you can save the data for all players and the server will shut down when all of the data has been saved (or when 30 seconds has been elapsed)

As for the last issue, this can be solved by implementing a retry system that is limited to a specific amount of retries. You will have to use pcall so you save the data in protected mode where errors that occur doesn't terminate further execution of the code but indicates whether or not it ran successfully

Also, it's easier to use a dictionary to save and retrieve the data. All together, here's what the code should be:

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
local Players = game:GetService("Players")

function SaveData(player)   
    local MAX_TRIES = 5
    local tries = 0

    local saveData = {
        Money = player.leaderstats.Money.Value;
    }

    repeat
        local success = pcall(function()
            ds:SetAsync("id_"..player.UserId, saveData)
        end)

                wait(6)

        tries += 1
    until success or tries >= 5
end

function GetData(player)
    local MAX_TRIES = 5
    local tries = 0
    local result

    repeat
        local success = pcall(function()
            result = ds:GetAsync("id_"..player.UserId)
        end)

                wait(7)
        tries += 1
    until success or tries >= 5

    return result
end

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = 0

    local data = GetData(player)
    if data then
        money.Value = data.Money
    end
end)

game.Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
    local player_list = Players:GetPlayers()
    local player_count = #player_list
    local saved_players = {}

    for _, player in ipairs(player_list) do
        task.spawn(function()
            SaveData(player)
            table.insert(saved_players, player)
        end)
    end

    while (#saved_players < player_count) do task.wait() end
end)
0
Okay so i tried your script and i tested it with a mate of mine but the weird thing is all the players money seem to be client sided we both joined i had 75 he had 50 i left and rejoined now we both had 0 but on his screen it still was increasing i am very confused loganmatt3 10 — 3y
0
I can tell that the type of script you used to increment the player's stats was a local script. Most of things done on the client will not replicate to the server in respect with the client-server model (FilteringEnabled). So it's as if the server didn't see the change in the stats. You should try using a script to increment the stats Rare_tendo 3000 — 3y
0
i am using a script not a localscript loganmatt3 10 — 3y
0
i found the issue word then somewhere was set as a comment i very smart yes loganmatt3 10 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Have you enabled Studio Access to API Services? To do so, go to the Roblox website's develop section and configure your game's experience. Click on "Enable Studio Access to API Services", and try again. EnzoTDZ_YT's code is probably the best one in this scenario though.

Log in to vote
0
Answered by 3 years ago

You are using GetAsync() to save the value. You should be using SetAsync()

game.Players.PlayerRemoving:Connect(function(player)
    ds:SetAsync("id_"..player.UserId, {player.leaderstats.Money.Value}) -- Change get to set to save the value.
end)
0
It does not work it keeps just resetting loganmatt3 10 — 3y
0
Try merging the leaderstat folder creation script with the load data script EnzoTDZ_YT 275 — 3y

Answer this question