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

Player Stats Not Saving?

Asked by 3 years ago

I am trying to save data from a folder in replicated storage, though my script doesn't seem to work as nothing gets saved... I am not sure what I did wrong here. The code will be posted here:


local DataStoreService = game:GetService('DataStoreService') local WeapStore = DataStoreService:GetDataStore('WeapStore') local repStore = game:GetService('ReplicatedStorage') local OwnedWeaps = repStore.WeapOwned game.Players.PlayerAdded:Connect(function(player) local myWeaps = OwnedWeaps:Clone() myWeaps.Parent = player local dataTable = myWeaps:GetChildren() for _,v in pairs(dataTable) do print(player.UserId..v.Name) local data local success, errormessage = pcall(function() data = WeapStore:GetAsync(player.UserId..v.Name) end) if success then v.Value = data else warn(errormessage) end end end) game.Players.PlayerRemoving:Connect(function(player) local weapValueFolder = player:FindFirstChild('WeapOwned') local dataTable = weapValueFolder:GetChildren() for _,v in pairs(dataTable) do print(player.UserId..v.Name) local success, errormessage = pcall(function() WeapStore:SetAsync(player.UserId..v.Name, v.Value) end) if success then print('Weapons successfully saved!') else warn(errormessage) end end end)
0
you also need to add a game:BindToClose() function to make sure to save everyone's data before the server shuts down nkminion 21 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

If you are testing and you are the only one in you server, the server shuts down when you leave and the code wont run. To prevent this, add another block of code that includes this

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do

        local Id = "player_"..player.UserId
        local Data = player.leaderstats.Cash.Value

            local success, errormessage = pcall(function()
            DataStore1:SetAsync(Id, Data)
                print("DataSaved")
            end)

        if success then
                print("DataSaved")
        elseif errormessage then
            print("There was a error")
warn(errormessage)
        end
    end
end)

Put this under your other code

Ad

Answer this question