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

Score points do not save when i shut down the server?

Asked by
Ben_B 9
4 years ago

Any points gained between the player joining the game and the server shutting down is lost, id there a fix?

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 Score = Instance.new("IntValue")
    Score.Name = "Score"
    Score.Parent = leaderstats
    local Credits = Instance.new("IntValue")
    Credits.Name = "Credits"
    Credits.Parent = leaderstats


    local data
    local data2
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId.."-Credits")
        data2 = myDataStore:GetAsync(player.UserId.."-Score")
    end)

        if success == true then
            Credits.Value = data
            Score.Value = data2
        end

end)
game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage pcall(function()
        myDataStore:SetAsync(player.UserId.."-Credits", player.leaderstats.Credits.Value)
        myDataStore:SetAsync(player.UserId.."-Score", player.leaderstats.Score.Value)
    end)
    if success == true then 
        print ("Data successfully saved")
    else
        print ("Error")
        warn(errormessage)
    end
end)

0
What is the error traceback? UHaveClout 1 — 4y
0
Well when I save in studio client it comes up with error, but data saves fine in game. It only loses when i shut down the game. Ben_B 9 — 4y
0
Give my answer a look. Ziffixture 6913 — 4y

3 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

This is because the Clients are abruptly disconnected. You need to open a window for the Server to allocate a short time frame for saving the data before closing the link. You can do this with the :BindToClose() method if game:

local Players = game:GetService("Players")

game:BindToClose(function()
    for _,Player in pairs(Players:GetPlayers()) do
        Player:Kick() --// Force PlayerRemoving for Data Save.
    end
end)

Note: The Server will open a window of 30 maximum seconds. If this period is exceeded, the functions will be discarded and the connection will proceed to collapse.


Remember:

This method will only attach the callback function for when the Server shut’s down, it will not call if a Player leaves. Although Data Saving can be done here, it is preferred that you don’t use :BindToClose() to do so, just invoke whichever signals or functions that are responsible for this within instead.

0
Is that in a new script, sorry Ive never seen this before Ben_B 9 — 4y
0
Ive put it at the end of the code and it has not saved Ben_B 9 — 4y
0
Place this in the same script, at the very bottom past the PlayerAdded. Try in-game. Ziffixture 6913 — 4y
0
Ive placed it at the end, published, tried in game and it doesn not save Ben_B 9 — 4y
0
The scripts in ServerScriptService if that makes a difference Ben_B 9 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Use game:BindToClose to prevent data loss when the server shuts down.

game:BindToClose(function()
    for _, players in next, game:GetService("Players"):GetPlayers() do
        -- Save data
    end
end)

NOTE: In order for DataStores to work in studio, turn on Enable Studio Access to API Services

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 Score = Instance.new("IntValue")
    Score.Name = "Score"
    Score.Parent = leaderstats
    local Credits = Instance.new("IntValue")
    Credits.Name = "Credits"
    Credits.Parent = leaderstats


    local data
    local data2
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId.."-Credits")
        data2 = myDataStore:GetAsync(player.UserId.."-Score")
    end)

        if success == true then
            Credits.Value = data
            Score.Value = data2
        end

end)
game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage pcall(function()
        myDataStore:SetAsync(player.UserId.."-Credits", player.leaderstats.Credits.Value)
        myDataStore:SetAsync(player.UserId.."-Score", player.leaderstats.Score.Value)
    end)
    if success == true then 
        print ("Data successfully saved")
    else
        print ("Error")
        warn(errormessage)
    end
end)
game:BindToClose(function()
    for _, players in next, game:GetService("Players"):GetPlayers() do
        -- Save data
    end
end)

Your full script should look like this.

0
Where would I put this in the code? Do i wrap the whole dataStore Ben_B 9 — 4y
0
At the end of the script is fine. Just make sure before loops. You should wrap Set and GetAsync in pcalls to prevent the script from yielding. UHaveClout 1 — 4y
0
Ive put it at the end and nothing has changed Ben_B 9 — 4y
0
Turn on Enable Studio Access to API Services if in studio UHaveClout 1 — 4y
View all comments (4 more)
0
I mean in the actual game when its shut down the data does not save. Ben_B 9 — 4y
0
What's your full script? UHaveClout 1 — 4y
0
Ive tried that and it does not work :/ is there anything i need enabled ? Ben_B 9 — 4y
0
Ive pasted that exact code in and it doesnt save Ben_B 9 — 4y
Log in to vote
0
Answered by
Ben_B 9
4 years ago

this is my full leaderboard and datastore script, ive tried putting both codes at the end and nothing has changed, do i need to wrap anything around it?

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 Score = Instance.new("IntValue")
    Score.Name = "Score"
    Score.Parent = leaderstats
    local Credits = Instance.new("IntValue")
    Credits.Name = "Credits"
    Credits.Parent = leaderstats


    local data
    local data2
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId.."-Credits")
        data2 = myDataStore:GetAsync(player.UserId.."-Score")
    end)

        if success == true then
            Credits.Value = data
            Score.Value = data2
        end

end)
game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage pcall(function()
        myDataStore:SetAsync(player.UserId.."-Credits", player.leaderstats.Credits.Value)
        myDataStore:SetAsync(player.UserId.."-Score", player.leaderstats.Score.Value)
    end)
    if success == true then 
        print ("Data successfully saved")
    else
        print ("Error")
        warn(errormessage)
    end
end)

Answer this question