Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Datastore request was added to queue?

Asked by 4 years ago
Edited 4 years ago

What could be causing this to happen?

DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests

here is the script:

local DataStore = game:GetService("DataStoreService")
local Level1 = DataStore:GetDataStore("Levels001")
local Beli11 = DataStore:GetDataStore("Beli001")
local Exp1 = DataStore:GetDataStore("Exp001")
local ExpNeed1 = DataStore:GetDataStore("ExpNeed001")


game.Players.PlayerAdded:Connect(function(Plr)
    local stats = Instance.new("Folder", Plr)
    stats.Name = "Data"
    --- Level System
    local Levels = Instance.new("IntValue", stats)
    Levels.Name = "Levels"
    Levels.Value = 1
    local Exp = Instance.new("IntValue", stats)
    Exp.Name = "Exp"
    Exp.Value = 0
    local ExpNeed = Instance.new("IntValue", stats)
    ExpNeed.Name = "ExpNeed"
    ExpNeed.Value = 200
    --- Money System
    local Beli = Instance.new("IntValue", stats)
    Beli.Name = "Gold"
    Beli.Value = 0
    --- Stats Text

---- Datastore ----
--- Levels
   Levels.Value = Level1:GetAsync(Plr.UserId) or Levels.Value
       Level1:SetAsync(Plr.UserId, Levels.Value)
      Levels.Changed:connect(function()
       Level1:SetAsync(Plr.UserId, Levels.Value)
   end)
--- Gold
   Beli.Value = Beli11:GetAsync(Plr.UserId) or Beli.Value
       Beli11:SetAsync(Plr.UserId, Beli.Value)
      Beli.Changed:connect(function()
       Beli11:SetAsync(Plr.UserId, Beli.Value)
   end)
--- Exp
   Exp.Value = Exp1:GetAsync(Plr.UserId) or Exp.Value
       Exp1:SetAsync(Plr.UserId, Exp.Value)
      Exp.Changed:connect(function()
       Exp1:SetAsync(Plr.UserId, Exp.Value)
   end)
--- ExpNeed
   ExpNeed.Value = ExpNeed1:GetAsync(Plr.UserId) or ExpNeed.Value
       ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
      ExpNeed.Changed:connect(function()
       ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
   end)

end)

game.Players.PlayerAdded:Connect(function(plr)
    wait(.1)
    local Exp = plr.Data.Exp
    local Levels = plr.Data.Levels
    local ExpNeed = plr.Data.ExpNeed


    while wait() do
        if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
            Levels.Value = Levels.Value + 1

            Exp.Value = Exp.Value - ExpNeed.Value
            ExpNeed.Value = ExpNeed.Value + 100
            game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
        end
    end
end)

game.Players.PlayerRemoving:connect(function(Player)
    Level1:SetAsync(Player.UserId, Player.Data.Levels.Value)
    Beli11:SetAsync(Player.UserId, Player.Data.Gold.Value)
    Exp1:SetAsync(Player.UserId, Player.Data.Exp.Value)
    ExpNeed1:SetAsync(Player.UserId, Player.Data.ExpNeed.Value)









end)

Problem solved, I spent the last few hours trying to make myself better with data store.

If you guys don't mind i would love some criticism on the new script.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("bloxbrawl")

local data = {
    ["Levels"] = 1,
    ["Exp"] = 0,
    ["ExpNeed"] = 200,
    ["Gold"] = 100
}

Players.PlayerAdded:Connect(function(Player)
    local DataFromStore = nil

    local stats = Instance.new("Folder")
    stats.Name = "Data"
    stats.Parent = Player

    for name, value in pairs(data) do
        local new = Instance.new("IntValue")
        new.Name = name
        new.Value = value
        new.Parent = stats

    end

    local s, e = pcall(function()
        DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
    end)

    if s then
        print("Getting Data For: " .. Player.Name)
        if DataFromStore then
            for name, value in pairs(DataFromStore) do
                Player.Data[name].Value = value
            end
            print("Data Loaded For: " .. Player.Name)
        else
            print("Created New Data For: " .. Player.Name)
        end
    else 
        warn("Error Getting Data For: " .. Player.Name)
    end
end)

game.Players.PlayerAdded:Connect(function(plr)
    wait(.1)
    local Exp = plr.Data.Exp
    local Levels = plr.Data.Levels
    local ExpNeed = plr.Data.ExpNeed


    while wait() do
        if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
            Levels.Value = Levels.Value + 1

            Exp.Value = Exp.Value - ExpNeed.Value
            ExpNeed.Value = ExpNeed.Value + 100
            game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
            end
            end
            end)

Players.PlayerRemoving:Connect(function(Player)
    local DataForStore = {}

    for name, value in pairs(Player.Data:GetChildren()) do
        DataForStore[value.Name] = value.Value
    end

    local success = pcall(TestDataStore.SetAsync, TestDataStore, "uid-" .. Player.UserId, DataForStore)

    if success then
        print("Successfully Saved Data For: " .. Player.Name)
    end
    end)

Should BindToClose be added aswell?

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I might know what's causing the problem. First of all, I wanna talk about pcall. pcall catches errors, and it doesn't break your script if an error occurs.

local success, errorMessage = pcall(function()
    Level1:SetAsync(Player.UserId.."-Levels", Player.Data.Levels.Value)
end)
if success then
    print("Data successfully saved!")
else
    warn("Error while saving data: "..errorMessage)
end

Secondly, I do not recommend that you use GetDataStore() for each value. From this wiki it shows that you can only use 60 + number of players x 10. To reduce requests, I recommend using an array (or table.)

local dataTable = {
    lvl = Levels.Value  
    xp = Exp.Value
    xpN = ExpNeed.Value
    beli = Beli.Value
}

and use the DataStore:SetAsync() as mentioned above. SetAsync can save tables, and thus reduces your requests.

0
Hey, There i appreciate your help. I did spend the last few hours improving the script, I have edited the post to include the new improved script. It works perfectly but as of being a new scripter i would love some feedback! thank you! JDT032589 6 — 4y
0
It's no problem. I'm happy to help people. Dovydas1118 1495 — 4y
Ad

Answer this question