CloutBl0x
(cloutblox)
March 24, 2023, 12:49am
#1
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)
4667hp
(Account Info)
March 24, 2023, 12:53am
#2
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
CloutBl0x
(cloutblox)
March 24, 2023, 9:52pm
#4
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
paetemc2
(Dragon_bloxy)
March 25, 2023, 3:36am
#5
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
CloutBl0x
(cloutblox)
March 25, 2023, 4:34am
#6
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
system
(system)
Closed
April 8, 2023, 7:34pm
#8
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.