in wich script do you have this error?
Wait nvm that was a wrong script I just deleted it
I think i found a fix, i have to do something real quick tho. Ill get back ASAP!
Ok thank you, don’t worry though!
Okay so in both the datastore and leaderboard script you save something with the same UserId when a player leaves.
Due to roblox their datastore limits, there HAS to be a 6 second interval between saving with the same key
So for example:
Since the leaderboard datastore is different from the normal datastore you can just change their keys.
In the normal datastore you save with the player.UserId
.
And in the leaderboard datastore you save with for example "leaderboard_"..player.UserId
. Since they both have different keys, it will not hit the datastore limits
Do not forget to also use the same key used when doing :GetAsync()
If you want to be ~100% safe from getting annoying errors you can also take a look at different saving methods like:
Try this:
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetOrderedDataStore("leaderboarddatastore")
local player = game:GetService("Players")
local coinsleaderboard = workspace:WaitForChild("Coinsleaderboard")
local frame = coinsleaderboard:WaitForChild("SurfaceGui"):WaitForChild("Frame"):WaitForChild("ScrollingFrame")
local UIGridLayout = frame:WaitForChild("UIGridLayout")
local tempclone = script:WaitForChild("Temp")
local playerisingame = {}
local function newplayer(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Coins = Instance.new("NumberValue", leaderstats)
Coins.Name = "Coins"
local coinsdata
pcall(function()
coinsdata = datastore:GetAsync(player.UserId.."_leaderboard")
end)
if coinsdata ~= nil then
Coins.Value = coinsdata
end
end
local function Updateguileaderboard()
local pages
pcall(function()
pages = datastore:GetSortedAsync(false,10)
end)
if pages ~= nil then
local currentpage = pages:GetCurrentPage()
for i, v in pairs(frame:GetChildren()) do
if v:IsA("Frame") then
v:Destroy()
end
end
for i, data in ipairs(currentpage) do
local username
pcall(function()
username = player:GetNameFromUserIdAsync(data["key"])
end)
if username ~= nil then
local tempframe = tempclone:Clone()
tempframe.Parent = frame
tempframe:WaitForChild("Rank").Text = "#"..i
tempframe:WaitForChild("Player").Text = username
tempframe:WaitForChild("Coins").Text = data["value"]
end
end
local size = Vector2.new(1, 0.1) * frame.AbsoluteSize
UIGridLayout.CellSize = UDim2.new(0, size.X, 0, size.Y)
frame.CanvasSize = UDim2.new(0, 0, 0, UIGridLayout.AbsoluteContentSize.Y)
end
end
local function playerremoving(player)
playerisingame[player] = nil
pcall(function()
local tempcoins = player:WaitForChild("leaderstats"):WaitForChild("Coins").Value
if tempcoins >= 1 then
datastore:SetAsync(player.UserId.."_leaderboard", tempcoins)
Updateguileaderboard()
end
end)
end
player.PlayerAdded:Connect(function(player)
if playerisingame[player] == nil then
newplayer(player)
end
end)
playerisingame = player:GetPlayers()
for i, v in pairs(playerisingame) do
newplayer()
end
player.PlayerRemoving:Connect(playerremoving)
Updateguileaderboard()
I am still confused how to change it in me script
Thats the wrong script I sent before I deleted that script this is script that I am using
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreV3")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("NumberValue", leaderstats)
Coins.Name = "Coins"
Coins.Parent = leaderstats
Coins.Value = 0
local donates = Instance.new("IntValue")
donates.Name = "R$ Donated"
donates.Parent = player
donates.Value = 0
local DJumps = Instance.new("NumberValue",player)
DJumps.Name = "DJumps"
local MaxJumps = Instance.new("NumberValue",DJumps)
MaxJumps.Name = "MaxDJumps"
MaxJumps.Value = 1
local Boost = Instance.new("NumberValue",DJumps)
Boost.Name = "Boost"
Boost.Value = MaxJumps.Value
local Need = Instance.new("NumberValue",DJumps)
Need.Name = 'Need'
Need.Value = 100
local Strength = Instance.new("NumberValue", leaderstats)
Strength.Name = "Strength"
Strength.Parent = leaderstats
Strength.Value = 0
local Capacity = Instance.new("NumberValue", player)
Capacity.Name = "Capacity"
Capacity.Parent = player
Capacity.Value = 10
local Diamonds = Instance.new("NumberValue", leaderstats)
Diamonds.Name = "Diamonds"
Diamonds.Parent = leaderstats
Diamonds.Value = 0
local Rank = Instance.new("IntValue", leaderstats)
Rank.Name = "Rank"
Rank.Parent = leaderstats
Rank.Value = "MiniFlyWeight"
local Level = Instance.new("NumberValue")
Level.Name = "Level"
Level.Parent = player
Level.Value = 1
local Exp = Instance.new("NumberValue")
Exp.Name = "Exp"
Exp.Parent = Level
Exp.Value = 0
local MaxExp = Instance.new("NumberValue")
MaxExp.Name = "MaxExp"
MaxExp.Parent = Level
MaxExp.Value = 100
Strength.Changed:Connect(function()
if Strength.Value >= Capacity.Value then
Strength.Value = Capacity.Value
end
end)
Exp.Changed:Connect(function(Val)
if Exp.Value >= MaxExp.Value then
Level.Value = Level.Value + 1
Exp.Value = 0
MaxExp.Value = MaxExp.Value + 10
end
end)
local key = player.UserId
local data
local success,msg = pcall(function()
data = DataStore:GetAsync(key)
end)
if data then
print("Data Loaded")
Strength.Value = data.S
Capacity.Value = data.C
Rank.Value = data.R
Diamonds.Value = data.D
Coins.Value = data.Cs
MaxJumps.Value = data.M
Need.Value = data.N
Level.Value = data.L
Exp.Value = data.E
MaxExp.Value = data.m
donates.Value = data.d
else
print("Error")
warn(msg)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
local data = {
C = player.Capacity.Value,
Cs = player.leaderstats.Coins.Value,
S = player.leaderstats.Strength.Value,
D = player.leaderstats.Diamonds.Value,
R = player.leaderstats.Rank.Value,
M = player.DJumps.MaxDJumps.Value,
N = player.DJumps.Need.Value,
L = player.Level.Value,
E = player.Level.Exp.Value,
m = player.Level.MaxExp.Value,
d = player.leaderstats.donates.Value,
}
local success,msg = pcall(function()
DataStore:SetAsync(key,data)
end)
if success then
print("Data Saved!")
end
end)
game.ReplicatedStorage:WaitForChild("Boost").OnServerEvent:Connect(function(player)
if player.leaderstats.Coins.Value >= player.DJumps.Need.Value then
player.leaderstats.Coins.Value -= player.DJumps.Need.Value
player.DJumps.Need.Value *= 10
player.DJumps.MaxDJumps.Value += 1
print(player.DJumps.MaxDJumps.Value)
else
print("Not Enough Coins!")
end
end)
Is this the only script that is doing datastore related things in your game now?
Sorry if I am confusing you but that is the script that I think keeps giving the errors
There is more but they are for something else
Okay there seems to be some confusion here, let me explain it more carefully.
In this script below you save with the key thats the UserId of the player. This script itself should work fine!
-- main datastore script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreV3")
game.Players.PlayerAdded:Connect(function(player)
local key = player.UserId
local data = DataStore:GetAsync(key)
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
local success,msg = pcall(function()
DataStore:SetAsync(key,32423424)
end)
if success then
print("Data Saved!")
else
warn(msg)
end
end)
Until we have another script that also does something with datastore. As you can see here we also save when the player leaves the server, and we also save it with the same exact key! Which is the user their UserId. so you see the problem here is that there are datastore limits. You can not save data with the same key too frequently. There has to be a 6 second interval. Which in this case there isnt cause we immediately save the data when the player leaves with the same key.
-- Second datastore script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreV3")
-- Some code bla bla bla
-- Oh yeah the player left!
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
local success,msg = pcall(function()
DataStore:SetAsync(key,32423424)
end)
if success then
print("Data Saved here too!")
else
warn(msg)
end
end)
This can be fixed by just using different keys in a script. Lets change the key in the second datastore script.
-- Second datastore script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreV3")
-- Some code bla bla bla
-- Oh yeah the player left!
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId.."_SecondKey" -- We changed the key here
local success,msg = pcall(function()
DataStore:SetAsync(key,32423424)
end)
if success then
print("Data Saved here too!")
else
warn(msg)
end
end)
And to get the data back we must use the same key!
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreV3")
game.Players.PlayerRemoving:Connect(function(player)
local key1 = player.UserId.."_SecondKey"
local data1 = DataStore:GetAsync(key1)
local key2 = player.UserId
local data2 = DataStore:GetAsync(key2)
print(data1)
print(data2)
end)
Simple solution. Datastore:updateasync() can handle it with no limits. Unless ur game is huge about 155 million people so you would use datastore 2
Add a wait(6) when function is called (task.wait(6) is better)
No, just make fewer requests to the DataStore(s) functions in your game.
I am not going to lie but I am still confused, I might not do a second datastore because my game isn’t released yet
I fixed the problem I was saving the same like stats value to many times in some other scripts, thank you for everyone that helped!