Hi, I’m developing my first datastore system, and I noticed it’s been crashing studio when I stop playing and saving data incorrectly. Basically, when I publish the game and run it, it crashes the client on stop. When it’s not published (meaning datastore doesn’t function), it doesn’t crash. I think it has to do with my BindToClose function. Thanks
playerCount = 0
playerCount = playerCount + 1
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Parent = leaderstats
coins.Name = "Coins"
local diamonds = Instance.new("IntValue")
diamonds.Parent = leaderstats
diamonds.Name = "Diamonds"
local level = Instance.new("IntValue")
level.Parent = leaderstats
level.Name = "Level"
local xp = game.ReplicatedStorage:WaitForChild("XP"):Clone()
xp.Parent = level
local xpFormula = 500 * (level.Value ^ 2) - (500 * level.Value)
if value >= xpFormula then
level.Value = level.Value + 1
xp.Value = xp.Value - xpFormula
diamonds.Value = diamonds.Value + 10
local wins = Instance.new("NumberValue")
wins.Parent = leaderstats
wins.Name = "Wins"
character.Humanoid.WalkSpeed = 16
if character:FindFirstChild("GameTag") then
player_data = dataStores:GetAsync(player.UserId.."-Coins")
player_data2 = dataStores:GetAsync(player.UserId.."-Diamonds")
player_data3 = dataStores:GetAsync(player.UserId.."-Levels")
player_data4 = dataStores:GetAsync(player.UserId.."-XP")
player_data5 = dataStores:GetAsync(player.UserId.."-Wins")
if player_data ~= nil then
coins.Value = player_data
coins.Value = 0
if player_data2 ~= nil then
diamonds.Value = player_data
diamonds.Value = 0
if player_data3 ~= nil then
level.Value = player_data
level.Value = 1
if player_data4 ~= nil then
xp.Value = player_data4
xp.Value = 0
if player_data5 ~= nil then
wins.Value = player_data5
wins.Value = 0
local bindableEvent = Instance.new("BindableEvent")
playerCount = playerCount - 1
while playerCount >= 0 do
You aren’t meant to keep instances pointlessly alive by use of this method. BindToClose simply offers you a window of time to a maximum of 30 seconds to perform any functionality post-instance closure. If you don’t need any post-instance closure functionality to happen, you don’t really have a reason to use BindToClose.
In regards to the crash issue, I can almost guarantee that the BindToClose function is causing the problem. In terms of the code overall, I would suggest that you give it an entire makeover. Here are some things I’d like to point out:
Always try to localise your variables. The playerCount variable doesn’t really need to be global. Just add “local” to the beginning.
The playerCount upvalue, in this case, is not necessary.
Set properties first before the parent. It’s generally good practice to have your instances already set up before you drop them into a container.
Perhaps you’d like to create a function to streamline the creation of stats? Something akin to CreateStat("Level", DefaultValue) or something. Not that necessary, but I personally like to do this if I need to perform certain functions multiple times.
Instead of creating a new key for every item of data (which is inefficient in itself), create a key that uses the player’s UserId and instead save a table of data to that key. Your current method has quite a few flaws, including the fact that you use 5 Get/Set requests every time a save/load is needed. This is exhaustive to the budget and will result in throttling.
You aren’t making use of pcalls correctly. For such calls, you will want to utilise the success boolean returned from a pcall as well as any returned arguments. You can create your own error and no-data handlers from this. The current implementation may see data loss in the future.
The existence of the BindableEvent is pointless.
UpdateAsync should typically be used in place of SetAsync, as UpdateAsync respects previous values as well as any data being queued from other instances and places. SetAsync should only be used if you need to force data to a key, otherwise use UpdateAsync.
I’d be happy to provide you some code samples based on the above points, though I will not provide an entire DataStore refactor. It’s up to you how to interpret this advice and use or leave it.