Datastore script filling queue

For some reason, my datastore script is filling the queue every time I leave testing.

How can I make it not break the queue?

local dataStore = game:GetService("DataStoreService")
local statSave = dataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local minsPlayed = Instance.new("IntValue", leaderstats)
	minsPlayed.Name = "Minutes"
	minsPlayed.Value = statSave:GetAsync(player.UserId) or 0
	statSave:SetAsync(player.UserId, minsPlayed.Value)

	print(player.Name .. "'s Minutes were loaded.")

	minsPlayed.Changed:Connect(function()
		statSave:SetAsync(player.UserId, minsPlayed.Value)
	end)

	while wait(60) do
		minsPlayed.Value = minsPlayed.Value + 1
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	statSave:SetAsync(player.UserId, player.leaderstats.Minutes.Value)
end)

You don’t need to save it every time the value changes.

local dataStore = game:GetService("DataStoreService")
local statSave = dataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local minsPlayed = Instance.new("IntValue", leaderstats)
	minsPlayed.Name = "Minutes"
	minsPlayed.Value = statSave:GetAsync(player.UserId) or 0

	print(player.Name .. "'s Minutes were loaded.")

	while task.wait(60) do
		minsPlayed.Value += 1
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	statSave:SetAsync(player.UserId, player.leaderstats.Minutes.Value)
end)

You forgot the bind to close! This won’t work if the last player leaves the game.

1 Like

would I use game:BindToClose for this?

edit 1:

would this work?

local dataStore = game:GetService("DataStoreService")
local statSave = dataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local minsPlayed = Instance.new("IntValue", leaderstats)
	minsPlayed.Name = "Minutes"
	minsPlayed.Value = statSave:GetAsync(player.UserId) or 0

	print(player.Name .. "'s Minutes were loaded.")

	while task.wait(60) do
		minsPlayed.Value += 1
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	statSave:SetAsync(player.UserId, player.leaderstats.Minutes.Value)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		statSave:SetAsync(player.UserId, player.leaderstats.Minutes.Value)		
	end
end)

edit 2:

queue is still filling upon every leave

i’m pretty sure it would work fine without the bind to close legit just copy and paste this code:

local dataStore = game:GetService("DataStoreService")
local statSave = dataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local minsPlayed = Instance.new("IntValue", leaderstats)
	minsPlayed.Name = "Minutes"
	minsPlayed.Value = statSave:GetAsync(player.UserId) or 0

	print(player.Name .. "'s Minutes were loaded.")

	while wait(60) do
		minsPlayed.Value = minsPlayed.Value + 1
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	statSave:SetAsync(player.UserId, player.leaderstats.Minutes.Value)
end)
1 Like

Thank you.

more characters are needed

Seems odd that it’s not needed. In every circumstance, I’ve always needed it (but I needed to save a table. Numbers might be different).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.