You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want the script to update a remote function and save it.
What is the issue? Include screenshots / videos if possible!
There are too many datastore requests and I don’t know how to reduce them. Error:DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 1670327072
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried getting help from other communities
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RebirthFunction = ReplicatedStorage:WaitForChild("Rebirth")
local MaxLevel = 5
local Abbreviations = {
"";
"K";
"M";
"B";
"T";
"Q";
"Qi";
"Sx";
"Sp";
"Oc";
"N";
"D";
"∞";
}
local function formatJumpHeight(jumpHeight)
for i = 1, #Abbreviations do
if jumpHeight < 10 ^ (i * 3) then
if Abbreviations[i] == "∞" then
return "∞"
else
return math.floor(jumpHeight / ((10 ^ ((i - 1) * 3)) / 100)) / 100 .. Abbreviations[i]
end
elseif tostring(jumpHeight) == "inf" then
return "∞"
end
end
end
-- Function to update player data in DataStore
local function updateDataStore(player)
local success, errorMessage = pcall(function()
local leaderstats = player:WaitForChild("leaderstats")
local jumpHeight = player.Character.Humanoid.JumpHeight
local winsValue = leaderstats:WaitForChild("Wins")
local rebirthsValue = leaderstats:WaitForChild("Rebirths")
local maxLevel = MaxLevel -- Assuming MaxLevel is a global variable
local data = {
JumpHeight = jumpHeight,
Wins = winsValue.Value,
Rebirths = rebirthsValue.Value,
MaxLevel = maxLevel
}
myDataStore:SetAsync(player.UserId, data)
end)
if success then
print("Data is saved for " .. player.Name)
else
print("Error when saving data for " .. player.Name .. ": " .. errorMessage)
end
end
-- Function to load player data from DataStore
local function loadDataStore(player)
local success, errorMessage = pcall(function()
local data = myDataStore:GetAsync(player.UserId) or {}
local jumpHeight = data.JumpHeight
local wins = data.Wins
local rebirths = data.Rebirths
local maxLevel = data.MaxLevel
if jumpHeight ~= nil then
player.Character.Humanoid.JumpHeight = jumpHeight
end
if wins ~= nil then
local leaderstats = player:WaitForChild("leaderstats")
local winsValue = leaderstats:WaitForChild("Wins")
winsValue.Value = wins
end
if rebirths ~= nil then
local leaderstats = player:WaitForChild("leaderstats")
local rebirthsValue = leaderstats:WaitForChild("Rebirths")
rebirthsValue.Value = rebirths
end
if maxLevel ~= nil then
MaxLevel = maxLevel
end
end)
if not success then
print("Error when loading data for " .. player.Name .. ": " .. errorMessage)
end
end
-- Function to update leaderstats with JumpPower and Wins value
local function updateLeaderstats(player)
-- Find or create the leaderstats folder in the player
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
-- Find or create the JumpHeight and Wins values in leaderstats
local jumpHeightValue = leaderstats:FindFirstChild("JumpHeight")
if not jumpHeightValue then
jumpHeightValue = Instance.new("StringValue")
jumpHeightValue.Name = "JumpHeight"
jumpHeightValue.Parent = leaderstats
end
local winsValue = leaderstats:FindFirstChild("Wins")
if not winsValue then
winsValue = Instance.new("IntValue")
winsValue.Name = "Wins"
winsValue.Parent = leaderstats
end
local RebirthsValue = leaderstats:FindFirstChild("Rebirths")
if not RebirthsValue then
RebirthsValue = Instance.new("IntValue")
RebirthsValue.Name = "Rebirths"
RebirthsValue.Parent = leaderstats
end
RebirthFunction.OnServerInvoke = function(player)
if player.leaderstats.Wins.Value >= MaxLevel then
player.leaderstats.Rebirths.Value = player.leaderstats.Rebirths.Value + 1
player.leaderstats.Wins.Value = 0
player.Character.Humanoid.JumpHeight = 0
updateDataStore(player) -- call the updateDataStore function to save the data
end
return false
end
-- Update the JumpHeight and Wins values in leaderstats with abbreviated format
jumpHeightValue.Value = formatJumpHeight(player.Character.Humanoid.JumpHeight)
winsValue.Value = player:WaitForChild("leaderstats").Wins.Value
if RebirthsValue then
RebirthsValue.Value = player.leaderstats.Rebirths.Value
end
end
-- Connect PlayerAdded event
game.Players.PlayerAdded:Connect(function(plr)
-- Player Joined
-- Character Spawned
plr.CharacterAdded:Connect(function(char)
char.Humanoid.UseJumpPower = false
char.Humanoid.JumpHeight = 0
-- Load JumpPower value from DataStore
loadDataStore(plr)
end)
end)
-- Main loop
while true do
wait(1) -- Wait for 1 second
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character then
-- Update leaderstats with new JumpHeight value
updateLeaderstats(player)
-- Update DataStore with new JumpHeight value if 5 seconds have passed since the last update
updateDataStore(player)
player.Character.Humanoid.JumpHeight += 1
-- Add the following line to increase JumpHeight by the value of Wins
player.Character.Humanoid.JumpHeight += player.leaderstats.Wins.Value
player.Character.Humanoid.JumpHeight += player.leaderstats.Rebirths.Value
end
end
end
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
In your main loop, every 1 second, you are saving every players data which is way more frequently that it should be done. Even saving data on Changed is way to frequent.
Either save the data when the player leaves or on a scheduled, (ie 60s interval) or use something like ProfileService or Datastore2 methods for data sving/retrieval.
it didn’t work I got this error in the pcall: Error when saving data for bigboi_4578: ServerScriptService.Main Handler:42: attempt to index nil with ‘Humanoid’ At this part:
local function updateDataStore(player)
local success, errorMessage = pcall(function()
local leaderstats = player:WaitForChild("leaderstats")
local jumpHeight = player.Character.Humanoid.JumpHeight
local winsValue = leaderstats:WaitForChild("Wins")
local rebirthsValue = leaderstats:WaitForChild("Rebirths")
local maxLevel = MaxLevel -- Assuming MaxLevel is a global variable
local data = {
JumpHeight = jumpHeight,
Wins = winsValue.Value,
Rebirths = rebirthsValue.Value,
MaxLevel = maxLevel
}
myDataStore:SetAsync(player.UserId, data)
end)
if success then
print("Data is saved for " .. player.Name)
else
print("Error when saving data for " .. player.Name .. ": " .. errorMessage)
end
end
The player.Character reference is just a pointer to the character object in the workspace. You will need to define the character first, before referencing the Humanoid:
local character = player.Character
local jumpHeight = character.Humanoid.JumpHeight
First, run this script and wait for one minute to see how much requests you are making per minute:
New code
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RebirthFunction = ReplicatedStorage:WaitForChild("Rebirth")
local setasyncs = 0
local getasyncs = 0
local MaxLevel = 5
local Abbreviations = {
"";
"K";
"M";
"B";
"T";
"Q";
"Qi";
"Sx";
"Sp";
"Oc";
"N";
"D";
"∞";
}
local function formatJumpHeight(jumpHeight)
for i = 1, #Abbreviations do
if jumpHeight < 10 ^ (i * 3) then
if Abbreviations[i] == "∞" then
return "∞"
else
return math.floor(jumpHeight / ((10 ^ ((i - 1) * 3)) / 100)) / 100 .. Abbreviations[i]
end
elseif tostring(jumpHeight) == "inf" then
return "∞"
end
end
end
-- Function to update player data in DataStore
local function updateDataStore(player)
local success, errorMessage = pcall(function()
local leaderstats = player:WaitForChild("leaderstats")
local jumpHeight = player.Character.Humanoid.JumpHeight
local winsValue = leaderstats:WaitForChild("Wins")
local rebirthsValue = leaderstats:WaitForChild("Rebirths")
local maxLevel = MaxLevel -- Assuming MaxLevel is a global variable
local data = {
JumpHeight = jumpHeight,
Wins = winsValue.Value,
Rebirths = rebirthsValue.Value,
MaxLevel = maxLevel
}
myDataStore:SetAsync(player.UserId, data)
setasyncs += 1
end)
end
-- Function to load player data from DataStore
local function loadDataStore(player)
local success, errorMessage = pcall(function()
local data = myDataStore:GetAsync(player.UserId) or {}
getasyncs += 1
local jumpHeight = data.JumpHeight
local wins = data.Wins
local rebirths = data.Rebirths
local maxLevel = data.MaxLevel
if jumpHeight ~= nil then
player.Character.Humanoid.JumpHeight = jumpHeight
end
if wins ~= nil then
local leaderstats = player:WaitForChild("leaderstats")
local winsValue = leaderstats:WaitForChild("Wins")
winsValue.Value = wins
end
if rebirths ~= nil then
local leaderstats = player:WaitForChild("leaderstats")
local rebirthsValue = leaderstats:WaitForChild("Rebirths")
rebirthsValue.Value = rebirths
end
if maxLevel ~= nil then
MaxLevel = maxLevel
end
end)
if not success then
print("Error when loading data for " .. player.Name .. ": " .. errorMessage)
end
end
-- Function to update leaderstats with JumpPower and Wins value
local function updateLeaderstats(player)
-- Find or create the leaderstats folder in the player
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
-- Find or create the JumpHeight and Wins values in leaderstats
local jumpHeightValue = leaderstats:FindFirstChild("JumpHeight")
if not jumpHeightValue then
jumpHeightValue = Instance.new("StringValue")
jumpHeightValue.Name = "JumpHeight"
jumpHeightValue.Parent = leaderstats
end
local winsValue = leaderstats:FindFirstChild("Wins")
if not winsValue then
winsValue = Instance.new("IntValue")
winsValue.Name = "Wins"
winsValue.Parent = leaderstats
end
local RebirthsValue = leaderstats:FindFirstChild("Rebirths")
if not RebirthsValue then
RebirthsValue = Instance.new("IntValue")
RebirthsValue.Name = "Rebirths"
RebirthsValue.Parent = leaderstats
end
RebirthFunction.OnServerInvoke = function(player)
if player.leaderstats.Wins.Value >= MaxLevel then
player.leaderstats.Rebirths.Value = player.leaderstats.Rebirths.Value + 1
player.leaderstats.Wins.Value = 0
player.Character.Humanoid.JumpHeight = 0
updateDataStore(player) -- call the updateDataStore function to save the data
end
return false
end
-- Update the JumpHeight and Wins values in leaderstats with abbreviated format
jumpHeightValue.Value = formatJumpHeight(player.Character.Humanoid.JumpHeight)
winsValue.Value = player:WaitForChild("leaderstats").Wins.Value
if RebirthsValue then
RebirthsValue.Value = player.leaderstats.Rebirths.Value
end
end
-- Connect PlayerAdded event
game.Players.PlayerAdded:Connect(function(plr)
-- Player Joined
-- Character Spawned
plr.CharacterAdded:Connect(function(char)
char.Humanoid.UseJumpPower = false
char.Humanoid.JumpHeight = 0
-- Load JumpPower value from DataStore
loadDataStore(plr)
end)
end)
coroutine.resume(coroutine.create(function()
while task.wait(60) do
print("Set async requests: "..tostring(setasyncs).."/"..tostring(60+(#game.Players:GetPlayers()*10)))
print("Get async requests: "..tostring(getasyncs).."/"..tostring(60+(#game.Players:GetPlayers()*10)))
setasyncs = 0
getasyncs = 0
end
end))
-- Main loop
while true do
wait(1) -- Wait for 1 second
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character then
-- Update leaderstats with new JumpHeight value
updateLeaderstats(player)
-- Update DataStore with new JumpHeight value if 5 seconds have passed since the last update
updateDataStore(player)
player.Character.Humanoid.JumpHeight += 1
-- Add the following line to increase JumpHeight by the value of Wins
player.Character.Humanoid.JumpHeight += player.leaderstats.Wins.Value
player.Character.Humanoid.JumpHeight += player.leaderstats.Rebirths.Value
end
end
end
No, that gave me more of the errors. My original script has no errors but I want to have more room just incase I need to update more stuff for datastores so I am trying to reduce the amount of requests I use.
What did it print in the output? I am trying to help you reduce the amount of requests you are making, so we need to have a way to track how much requests you are making. The limit for setasync and getasync is both 60 per minute + number of players x 10. Try this code out, and tell me what is prints in the output after 60 seconds:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RebirthFunction = ReplicatedStorage:WaitForChild("Rebirth")
local setasyncs = 0
local getasyncs = 0
local MaxLevel = 5
local Abbreviations = {
"";
"K";
"M";
"B";
"T";
"Q";
"Qi";
"Sx";
"Sp";
"Oc";
"N";
"D";
"∞";
}
local cache = {}
local function formatJumpHeight(jumpHeight)
for i = 1, #Abbreviations do
if jumpHeight < 10 ^ (i * 3) then
if Abbreviations[i] == "∞" then
return "∞"
else
return math.floor(jumpHeight / ((10 ^ ((i - 1) * 3)) / 100)) / 100 .. Abbreviations[i]
end
elseif tostring(jumpHeight) == "inf" then
return "∞"
end
end
end
-- Function to update player data in DataStore
local function updateDataStore(player)
local success, errorMessage = pcall(function()
local leaderstats = player:WaitForChild("leaderstats")
local jumpHeight = player.Character.Humanoid.JumpHeight
local winsValue = leaderstats:WaitForChild("Wins")
local rebirthsValue = leaderstats:WaitForChild("Rebirths")
local maxLevel = MaxLevel -- Assuming MaxLevel is a global variable
local data = {
JumpHeight = jumpHeight,
Wins = winsValue.Value,
Rebirths = rebirthsValue.Value,
MaxLevel = maxLevel
}
myDataStore:SetAsync(player.UserId, data)
setasyncs += 1
end)
end
-- Function to load player data from DataStore
local function loadDataStore(player)
local success, errorMessage = pcall(function()
local data = myDataStore:GetAsync(player.UserId) or {}
getasyncs += 1
local jumpHeight = data.JumpHeight
local wins = data.Wins
local rebirths = data.Rebirths
local maxLevel = data.MaxLevel
if jumpHeight ~= nil then
cache[player.Name] = jumpHeight
end
if wins ~= nil then
local leaderstats = player:WaitForChild("leaderstats")
local winsValue = leaderstats:WaitForChild("Wins")
winsValue.Value = wins
end
if rebirths ~= nil then
local leaderstats = player:WaitForChild("leaderstats")
local rebirthsValue = leaderstats:WaitForChild("Rebirths")
rebirthsValue.Value = rebirths
end
if maxLevel ~= nil then
MaxLevel = maxLevel
end
end)
if not success then
print("Error when loading data for " .. player.Name .. ": " .. errorMessage)
end
end
-- Function to update leaderstats with JumpPower and Wins value
local function updateLeaderstats(player)
-- Find or create the leaderstats folder in the player
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
-- Find or create the JumpHeight and Wins values in leaderstats
local jumpHeightValue = leaderstats:FindFirstChild("JumpHeight")
if not jumpHeightValue then
jumpHeightValue = Instance.new("StringValue")
jumpHeightValue.Name = "JumpHeight"
jumpHeightValue.Parent = leaderstats
end
local winsValue = leaderstats:FindFirstChild("Wins")
if not winsValue then
winsValue = Instance.new("IntValue")
winsValue.Name = "Wins"
winsValue.Parent = leaderstats
end
local RebirthsValue = leaderstats:FindFirstChild("Rebirths")
if not RebirthsValue then
RebirthsValue = Instance.new("IntValue")
RebirthsValue.Name = "Rebirths"
RebirthsValue.Parent = leaderstats
end
RebirthFunction.OnServerInvoke = function(player)
if player.leaderstats.Wins.Value >= MaxLevel then
player.leaderstats.Rebirths.Value = player.leaderstats.Rebirths.Value + 1
player.leaderstats.Wins.Value = 0
player.Character.Humanoid.JumpHeight = 0
end
return false
end
-- Update the JumpHeight and Wins values in leaderstats with abbreviated format
jumpHeightValue.Value = formatJumpHeight(player.Character.Humanoid.JumpHeight)
winsValue.Value = player:WaitForChild("leaderstats").Wins.Value
if RebirthsValue then
RebirthsValue.Value = player.leaderstats.Rebirths.Value
end
end
-- Connect PlayerAdded event
game.Players.PlayerAdded:Connect(function(plr)
-- Player Joined
loadDataStore(plr)
-- Character Spawned
plr.CharacterAdded:Connect(function(char)
char.Humanoid.UseJumpPower = false
char.Humanoid.JumpHeight = cache[plr.Name]
end)
end)
coroutine.resume(coroutine.create(function()
while task.wait(60) do
print("Set async requests: "..tostring(setasyncs).."/"..tostring(60+(#game.Players:GetPlayers()*10)))
print("Get async requests: "..tostring(getasyncs).."/"..tostring(60+(#game.Players:GetPlayers()*10)))
setasyncs = 0
getasyncs = 0
end
end))
-- Main loop
while true do
task.wait(1) -- Wait for 1 second
for _, player in pairs(game.Players:GetPlayers()) do
task.wait(6)
-- Update leaderstats with new JumpHeight value
updateLeaderstats(player)
-- Update DataStore with new JumpHeight value if 5 seconds have passed since the last update
updateDataStore(player)
loadDataStore(player)
if player.Character then
player.Character.Humanoid.JumpHeight += 1
-- Add the following line to increase JumpHeight by the value of Wins
player.Character.Humanoid.JumpHeight += player.leaderstats.Wins.Value
player.Character.Humanoid.JumpHeight += player.leaderstats.Rebirths.Value
end
end
end
Screenshot / copy paste what you see in the output (both errors and logs)
Nothing works at all and it is full of issues so I don’t think it is worth it if I show you the output. However, go back to the original script that I have and just try to limit how many times I use the functions updatedatastore, loaddatastore, and updateleaderstats. (especially in the while loop) Basically calling on those functions too often would result in too many requests.
Dude what do you mean “it’s not worth it to show you the output”??? We need to know how often you are requesting the datastore in order to
the amount of requests you are making per minute.
Show me the issue so I can fix it
That’s literally what my code does. Please check the output so we can know which datastore surpassed the datastore request limit.
I know that, and that’s what I just fixed in my game last week. What are the issues you are talking about, and it is definitely “worth it” to check the output. If you want to solve the issue of too many datastore requests, tell me what it shows in the output and also the “issues” you are having.
This is an example of what the output should look like:
As you can see, the get sorted async requests are over the limit, so I would expect a warning of it. However, the get async requests and the set async requests are not over the limit, so I wouldn’t get any errors or warnings on that one. I would say that it’s definitely worth it to check the output and see how many requests you are making so we can pinpoint the problem. How many requests are you amking for get async and set async?
I have 2 leaderboard in my game that uses ordered datastore. In the main handler(The script I showed you) I only used set async and get async once.
Leaderboard 1:
local ds = game:GetService("DataStoreService")
local coinsODS = ds:GetOrderedDataStore("CoinsStats")
local players = game:GetService("Players")
local timeUntilReset = 10
local Abbreviations = {
"";
"K";
"M";
"B";
"T";
"Q";
"Qi";
"Sx";
"Sp";
"Oc";
"N";
"D";
"∞";
}
local function formatJumpHeight(jumpHeight)
for i = 1, #Abbreviations do
if jumpHeight < 10 ^ (i * 3) then
if Abbreviations[i] == "∞" then
return "∞"
else
return math.floor(jumpHeight / ((10 ^ ((i - 1) * 3)) / 100)) / 100 .. Abbreviations[i]
end
elseif tostring(jumpHeight) == "inf" then
return "∞"
end
end
end
while wait(2) do
timeUntilReset = timeUntilReset - 1
script.Parent.Parent.ResetTime.Text = "Resetting in " .. timeUntilReset .. " seconds..."
if timeUntilReset == 0 then
timeUntilReset = 10
for i, plr in pairs(game.Players:GetPlayers()) do
coinsODS:SetAsync(plr.UserId, plr.Character.Humanoid.JumpHeight)
end
for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
if leaderboardRank.ClassName == "Frame" then
leaderboardRank:Destroy()
end
end
local success, errorMsg = pcall(function()
local data = coinsODS:GetSortedAsync(false, 50)
local coinsPage = data:GetCurrentPage()
for rankInLB, dataStored in ipairs(coinsPage) do
local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
local coins = dataStored.value
local template = script.Template:Clone()
template.Name = name .. "Leaderboard"
template.PlrName.Text = name
template.Rank.Text = "#" .. rankInLB
template.Coins.Text = formatJumpHeight(coins)
template.Parent = script.Parent
end
end)
if errorMsg then
print("Player : " .. players .. "Had an issue saving")
end
end
end
leaderboard 2:
local ds = game:GetService("DataStoreService")
local coinsODS = ds:GetOrderedDataStore("CoinsStats")
local players = game:GetService("Players")
local timeUntilReset = 10
local Abbreviations = {
"";
"K";
"M";
"B";
"T";
"Q";
"Qi";
"Sx";
"Sp";
"Oc";
"N";
"D";
"∞";
}
local function formatJumpHeight(jumpHeight)
for i = 1, #Abbreviations do
if jumpHeight < 10 ^ (i * 3) then
if Abbreviations[i] == "∞" then
return "∞"
else
return math.floor(jumpHeight / ((10 ^ ((i - 1) * 3)) / 100)) / 100 .. Abbreviations[i]
end
elseif tostring(jumpHeight) == "inf" then
return "∞"
end
end
end
while wait(1) do
timeUntilReset = timeUntilReset - 1
script.Parent.Parent.ResetTime.Text = "Resetting in " .. timeUntilReset .. " seconds..."
if timeUntilReset == 0 then
timeUntilReset = 10
for i, plr in pairs(game.Players:GetPlayers()) do
coinsODS:SetAsync(plr.UserId, plr.leaderstats.Wins.Value)
end
for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
if leaderboardRank.ClassName == "Frame" then
leaderboardRank:Destroy()
end
end
local success, errorMsg = pcall(function()
local data = coinsODS:GetSortedAsync(false, 50)
local coinsPage = data:GetCurrentPage()
for rankInLB, dataStored in ipairs(coinsPage) do
local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
local coins = dataStored.value
local template = script.Template:Clone()
template.Name = name .. "Leaderboard"
template.PlrName.Text = name
template.Rank.Text = "#" .. rankInLB
template.Coins.Text = formatJumpHeight(coins)
template.Parent = script.Parent
end
end)
if errorMsg then
print("Player : " .. players .. "Had an issue saving")
end
end
end