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

I need this to be able to save and load, but it shows no errors and isn't working. Can anyone Help?

Asked by 4 years ago

I tried a lot of resources, but I am unable to make the gold, tool hardness, tool power, or spaces save. I would like it to save when a player leaves, and load when a player joins. I tried to get it to save and load, but It still doesn't work, nor show any sort of error, so i don't know what's wrong. It would be a huge help if this could save/load. also, everything below this is my code. The script is in the ServerScriptService.

    -- Creates a leaderboard that shows player variables
   local Players = game:GetService("Players")
    local goldDataStore = game:GetService("DataStoreService"):GetDataStore("Data0")


 local function onPlayerJoin(player)

    local playerKey = "Player_" .. player.UserId

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



        local gold = Instance.new("IntValue", leaderstats)
        gold.Name = "Gold"

local startgold = 0


game.Players.PlayerAdded:Connect(function()
    local myGold= goldDataStore:GetAsync(playerKey,{player.leaderstats.Gold.Value
    })
        gold.Value = myGold
end)





        local Items= Instance.new("IntValue")
        Items.Name = "Items"
        Items.Value = 0
        Items.Parent = leaderstats



        local Spaces = Instance.new("IntValue")
        Spaces.Name = "Spaces"
        Spaces.Value = 2
        Spaces.Parent = leaderstats



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



     local Toolhardness = Instance.new("IntValue")
    Toolhardness.Name = "ToolHardness"
    Toolhardness.Value = 1
    Toolhardness.Parent = Values


     local ToolPower = Instance.new("IntValue")
    ToolPower.Name = "ToolPower"
    ToolPower.Value = 1
    ToolPower.Parent = Values


end


    -- Run onPlayerJoin when the PlayerAdded event fires


for _, player in pairs(Players:GetPlayers()) do
    onPlayerJoin(player)
end


  game.Players.PlayerAdded:Connect(onPlayerJoin)

   local Players = game:GetService("Players")


    local goldDataStore = game:GetService("DataStoreService"):GetDataStore("Data0")

local function Saveonleave(player)

       local playerKey = "Player_" .. player.UserId

       goldDataStore:SetAsync(playerKey,{
    player.leaderstats.Gold.Value
    })


end

game.Players.PlayerRemoving:Connect(Saveonleave)



1 answer

Log in to vote
0
Answered by
AspectW 431 Moderation Voter
4 years ago

Sometimes, SetAsync and GetAsync can fail to retrieve data, that is why you should make your own retrying mechanism, here's an example of one.

local success = false
local tries = 0

while not success and tries < 4 do
    success, data = pcall(function()
        return DataStoreService:GetAsync("whatever")
    end)
    wait(1.5 * tries)
end
if success then
    print("Loaded player's data")
else
    print("Failed to retrieve player's data")
end
0
Thanks. I was just having a rough time getting the save function to work, and, again, thanks. lormandillis 2 — 4y
Ad

Answer this question