Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

3 comments

0

What is the error traceback?

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.

0

Give my answer a look.

3 answers

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.

5 comments

0

Is that in a new script, sorry Ive never seen this before

0

Ive put it at the end of the code and it has not saved

0

Place this in the same script, at the very bottom past the PlayerAdded. Try in-game.

0

Ive placed it at the end, published, tried in game and it doesn not save

0

The scripts in ServerScriptService if that makes a difference

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.

8 comments

0

Where would I put this in the code? Do i wrap the whole dataStore

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.

0

Ive put it at the end and nothing has changed

0

Turn on Enable Studio Access to API Services if in studio

0

I mean in the actual game when its shut down the data does not save.

0

What's your full script?

0

Ive tried that and it does not work :/ is there anything i need enabled ?

0

Ive pasted that exact code in and it doesnt save

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.