I recently started learning DataStore and am trying to implement it into a basic obby game.
After some investigation, I seem to have the same problem as was posted here. However, there doesn’t seem to be any solution to how to get DataStore to save consistently in studio.
The Data saves when I play the game, but in Studio it only saves my changes sometimes. I have tried switching to the server mode before making the changes and still have the same issue.
Here is my code to save the number of times a player has completed the obby:
local completionPart = game.Workspace.MiniObby.CompletionPart
local cooldown = false
local DSS = game:GetService("DataStoreService")
local completionsData = DSS:GetDataStore("completionsData")
completionPart.Touched:Connect(function(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid and cooldown == false then
cooldown = true
humanoid.Health = 0
local player = game:GetService("Players"):FindFirstChild(part.Parent.Name)
player.Team = game.Teams.Jumps
player.leaderstats.Completions.Value += 1
wait(2)
cooldown = false
end
end)
game:GetService("Players").PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = "leaderstats"
local Completions = Instance.new("IntValue")
Completions.Parent = leaderstats
Completions.Name = "Completions"
local Key = plr.UserId.."-Completions"
local data
local success, errorMessage = pcall(function()
data = completionsData:GetAsync(Key)
end)
if success then
Completions.Value = data
else
Completions.Value = 0
end
end)
game:GetService("Players").PlayerRemoving:Connect(function(plr)
local Key = plr.UserId.."-Completions"
local data = plr.leaderstats.Completions.Value
local success, errorMessage = pcall(function()
completionsData:SetAsync(Key, data)
end)
if success then
print("Data Saved")
else
print("Error While Saving Data")
warn(errorMessage)
end
end)
I believe the issue is with studio closing the server before it can finish saving.
The solution for this is to use bind to close, It will make sure the function fires before the server closes.
This isn’t only possible in studio, sometimes if a player is the last to leave a running server it can fail to save if you don’t use bind to close.
I added on to your code at the bottom to add a bind to close function.
here:
local completionPart = game.Workspace.MiniObby.CompletionPart
local cooldown = false
local DSS = game:GetService("DataStoreService")
local completionsData = DSS:GetDataStore("completionsData")
completionPart.Touched:Connect(function(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid and cooldown == false then
cooldown = true
humanoid.Health = 0
local player = game:GetService("Players"):FindFirstChild(part.Parent.Name)
player.Team = game.Teams.Jumps
player.leaderstats.Completions.Value += 1
wait(2)
cooldown = false
end
end)
game:GetService("Players").PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = "leaderstats"
local Completions = Instance.new("IntValue")
Completions.Parent = leaderstats
Completions.Name = "Completions"
local Key = plr.UserId.."-Completions"
local data
local success, errorMessage = pcall(function()
data = completionsData:GetAsync(Key)
end)
if success then
Completions.Value = data
else
Completions.Value = 0
end
end)
game:GetService("Players").PlayerRemoving:Connect(function(plr)
local Key = plr.UserId.."-Completions"
local data = plr.leaderstats.Completions.Value
local success, errorMessage = pcall(function()
completionsData:SetAsync(Key, data)
end)
if success then
print("Data Saved")
else
print("Error While Saving Data")
warn(errorMessage)
end
end)
-- ADDED, It runs when the game closes
game:BindToClose(function()
for i, plr in pairs(game.Players:GetPlayers()) do
local Key = plr.UserId.."-Completions"
local data = plr.leaderstats.Completions.Value
local success, errorMessage = pcall(function()
completionsData:SetAsync(Key, data)
end)
if success then
print("Data Saved")
else
print("Error While Saving Data")
warn(errorMessage)
end
end
end)
Sorry if this was explained horribly, I’m bad at explaining things XD
Only worry now is that I get a warning message: DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 27956488-Completions
and it takes about 5-10 seconds longer to stop a test
Good!
The warning message is because it sometimes attempts to save twice, It shouldn’t affect anything.
It just means that the datastore hit its limit and will wait a bit before saving the next one, but since it’s just saving a copy I think it will be fine. (I have the same warning in my game and it has yet to break anything).
But you could add code to check if the players data has already been saved by player leaving event and prevent it from saving again in bind to close.
As for the studio taking longer to close, It will always take longer to close in-studio because it has to save before ending the test. But you could add some code to detect if you are in studio and not save