I’ve been working on a datastore recently, and I ran into two issues. First, I can successfully load data, but for some reason I can only save it manually. Autosaving when the player leaves the server just doesn’t work despite using the same code as the manual save. I noticed that it usually takes around 5-ish seconds for the data to be successfully saved, so I’m thinking the player is disconnecting before the game can finish saving, maybe?
Here’s the code for this:
local DS = game:GetService("DataStoreService")
local dataScript = require(game.ReplicatedStorage:WaitForChild("savingDataTest"))
local VERSION = "1.0.0"
local playerStuff = {}
game.Players.PlayerAdded:Connect(function(Player : Player) --This function loads the data when the player joins.
playerStuff[Player.UserId] = dataScript.new(Player)
playerStuff[Player.UserId]:LoadData()
print(playerStuff[Player.UserId].Data.FrenzyTokens)
local fTokens = playerStuff[Player.UserId].Data.FrenzyTokens
game.ReplicatedStorage:WaitForChild("dataToClient"):FireClient(Player, fTokens)
end)
game.Players.PlayerRemoving:Connect(function(Player : Player) --This should save the player's data when they leave, but it isn't working.
game.ReplicatedStorage:WaitForChild("savingConnector"):FireClient(Player)
game.ReplicatedStorage:WaitForChild("dataToServer").OnServerEvent:Connect(function(plr, info)
print(info)
playerStuff[Player.UserId].Data.FrenzyTokens = info.fTokens
playerStuff[Player.UserId]:SaveData(info)
print("Saved! You have " .. info.fTokens .. " Frenzy Tokens.")
end)
end)
workspace.saveBtn.save.Triggered:Connect(function(Player : Player) --This should save when the player triggers a prompt. This part works.
game.ReplicatedStorage:WaitForChild("savingConnector"):FireClient(Player)
game.ReplicatedStorage:WaitForChild("dataToServer").OnServerEvent:Connect(function(plr, info)
print(info)
playerStuff[Player.UserId].Data.FrenzyTokens = info.fTokens
playerStuff[Player.UserId]:SaveData(info)
print("Saved! You have " .. info.fTokens .. " Frenzy Tokens.")
end)
end)
Secondly, while trying to troubleshoot this, I noticed that I get a warning telling me that if I add too many datastore requests to the queue, it will drop any new ones. It isn’t affecting me now, but should I be worrying about this? And if so, how can I fix this issue?
Any help here is appreciated, thank you!