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)
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.
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.
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.
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)