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

Why is the datastore telling me to send fewer requests?

Asked by 2 years ago
Edited 2 years ago

these are my data store scripts: Leaderstats:

game.StarterGui.ResetPlayerGuiOnSpawn = false
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:connect(function(Player)
    local leaderstats = Instance.new("Folder", Player)
    leaderstats.Name = "leaderstats"

    local val1 = Instance.new("StringValue",Player)
    val1.Name = 'GotPet'
    val1.Value = ''

    local val2 = Instance.new("StringValue",Player)
    val2.Name = 'OpenValue'
    val2.Value = ''

    local Gems = Instance.new("IntValue", leaderstats)
    Gems.Name = "Gems"
    Gems.Value = 0

    local Rebirths = Instance.new("IntValue", leaderstats)
    Rebirths.Name = "Rebirths"
    Rebirths.Value = 0

    local Coins = Instance.new("IntValue", leaderstats)
    Coins.Name = "Coins"
    Coins.Value = 0

    local playerUserId = "Player_" .. Player.UserId  --Gets player ID

    local Success, Result = pcall(function()
        return playerData:GetAsync(playerUserId)
    end)

    if Result then
        print("Data found!")

        Coins.Value = Result[1] -- // Since in the saving part, Coins are the one that is set as the first value of the table, we set Coins as [1]
        Rebirths.Value = Result[2]
        Gems.Value = Result[3]
    else
        print("DataStore working but No data found")
        warn(Result)
    end

end)

local function onPlayerExit(player)  --Runs when players exit



    local success, Err = pcall(function()
        print("Data Saved successfully")

        local playerUserId = "Player_" .. player.UserId

        playerData:SetAsync(playerUserId, {player.leaderstats.Coins.Value, player.leaderstats.Rebirths.Value, player.leaderstats.Gems.Value}) -- Now set as a table instead of individually

    end)



    if not success then
        warn(Err)
        warn('Could not save data!')
    end
end

game.Players.PlayerRemoving:Connect(onPlayerExit)

here is the Pet datastore:

local Players = game:GetService("Players")
local DS = game:GetService("DataStoreService"):GetDataStore("PetsSave")
local SaveOnChange = true
local Cooldown = 0 

local function Thread(f) 
    coroutine.resume(coroutine.create(f))
end

local Array = {
    ["Stats"] = {
    },
    ["PlayerValues"] = { 
    },
    ["Pets"] = {
        ['CurrentPet'] = '',
        ["Orange puppy"] = 'unowned',
        ["Dequinious"] = 'unowned',
        ["Gerreard"] = 'unowned',
        ["Amazing Pet"] = 'unowned',
        ["Uncommon Pet"] = 'unowned',
        ["Rare Pet"] = 'unowned',
        ["Unobtainable Pet"] = 'unowned',
        ["Lollie"] = 'unowned',
        ["Dog"] = 'unowned',
        ["Panda"] = 'unowned',
        ["Bunny"] = 'unowned',
        ["Cat"] = 'owned',
    }   
}

Players.PlayerAdded:connect(function(plr)
    for i,v in pairs(Array.PlayerValues) do 
        local x
        if type(v) == "number" then 
            x = Instance.new("NumberValue", plr) 
            x.Name = tostring(i) 
            x.Value = v 

        elseif type(v)=="string" then 
            x=Instance.new("StringValue",plr) 
            x.Name=tostring(i) 
            x.Value=v

        elseif type(v)=="boolean" then 
            x=Instance.new("BoolValue",plr) 
            x.Name=tostring(i) x.Value=v 

        end
    end
    local ID = plr.userId
    if DS:GetAsync("User_"..tostring(ID)) == nil then 
        print("Player "..plr.Name.."("..tostring(ID)..") has no data file, creating a new data file.")
        local NewData=DS:SetAsync("User_"..tostring(ID),Array) 
    end
    local Data = DS:GetAsync("User_"..tostring(ID)) 
    repeat wait() until Data ~= nil 
    print("User_"..tostring(ID).." Data Loading...")
    --Create leaderstats
    local Stats = Instance.new("IntValue",plr) 
    Stats.Name = "leaderstats" 
    --Create Pets
    local Pets = Instance.new("Configuration",plr) 
    Pets.Name = "Pets"
    Thread(function()
        for i,v in pairs(Array.Stats) do
            local x
            if type(v) == "number" then
                x = Instance.new("NumberValue",Stats) 
                x.Name = tostring(i)
                x.Value = Data.Stats[i] or v

            elseif type(v) == "string" then
                x = Instance.new("StringValue",Stats) 
                x.Name = tostring(i) 
                x.Value = Data.Stats[i] or v 

            elseif type(v) == "boolean" then 
                x = Instance.new("BoolValue",Stats) 
                x.Name = tostring(i) 
                x.Value = Data.Stats[i] or v 

            end
            if x == nil or x["Value"] == nil or SaveOnChange == false then
                return
            end
            local Deb = false
            x.Changed:connect(function(val)
                    Deb=true
                    local NewArray = {["Stats"]={};["Pets"]={}} 
                    Thread(function() 
                        for i,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do 
                            NewArray.Stats[v.Name] = v.Value 
                        end 
                    end)
                    for i,v in pairs(plr:WaitForChild("Pets"):GetChildren()) do 
                        NewArray.Pets[v.Name]=v.Value 
                    end
                    DS:UpdateAsync("User_"..tostring(ID), function(OldVal) 
                        local NewVal=NewArray or OldVal 
                        return NewVal 
                    end) 
                    wait(Cooldown) 
                    Deb=false
            end)    
        end
    end)
    for i,v in pairs(Array.Pets) do 
        local x
        if type(v) == "number" then 
            x = Instance.new("NumberValue", Pets) 
            x.Name = tostring(i) 
            x.Value = Data.Pets[i] or v 

        elseif type(v)=="string" then 
            x=Instance.new("StringValue",Pets) 
            x.Name=tostring(i) 
            x.Value=Data.Pets[i] or v

        elseif type(v)=="boolean" then 
            x=Instance.new("BoolValue",Pets) 
            x.Name=tostring(i) x.Value=Data.Pets[i] or v 

        end
        if x==nil or x["Value"]==nil or SaveOnChange==false then 
            return 
        end
        local Deb=false 
        x.Changed:connect(function(val) 
                Deb=true
                local NewArray={["Stats"]={};["Pets"]={}} 
                Thread(function() 
                    for i,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do 
                        NewArray.Stats[v.Name] = v.Value 
                    end 
                end)
                for i,v in pairs(plr:WaitForChild("Pets"):GetChildren()) do 
                    NewArray.Pets[v.Name]=v.Value 
                end
                DS:UpdateAsync("User_"..tostring(ID),function(OldVal) 
                    local NewVal = NewArray or OldVal 
                    return NewVal 
                end) 
                wait(Cooldown) 
                Deb=false 
        end) 
    end 
    print(plr.Name.."("..tostring(ID)..")'s Data is ready.") 
end)

Players.PlayerRemoving:connect(function(plr) 
    local ID=plr.userId
    local Data=DS:GetAsync("User_"..tostring(ID)) 
    if Data==nil then 
        return 
    end 
    local NewArray={["Stats"]={};["Pets"]={}}
    Thread(function() 
        for i,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do 
            NewArray.Stats[v.Name]=v.Value 
        end 
    end)
    for i,v in pairs(plr:WaitForChild("Pets"):GetChildren()) do 
        NewArray.Pets[v.Name]=v.Value 
    end
    DS:UpdateAsync("User_"..tostring(ID), function(OldVal) 
        local NewVal = NewArray or OldVal 
        return NewVal 
    end) 
end) 

local function SaveAll()
    for _,v in ipairs(Players:GetPlayers()) do
        Thread(function()
                local ID = v.userId
                local Data = DS:GetAsync("User_"..tostring(ID))
                if Data ~= nil then
                    local NewArray = {["Stats"] = {}; ["Pets"] = {}}
                    Thread(function()
                        for _,v2 in pairs(v:WaitForChild("leaderstats"):GetChildren()) do
                            NewArray.Stats[v2.Name] = v2.Value 
                        end
                    end)
                    for _,v2 in pairs(v:WaitForChild("Pets"):GetChildren()) do
                        NewArray.Pets[v2.Name] = v2.Value 
                    end
                    DS:UpdateAsync("User_"..tostring(ID), function(OldVal)
                        local NewVal=NewArray or OldVal
                        return NewVal
                    end)

            end
        end)
    end
end

game:BindToClose(function() 
    SaveAll() 
end)

1 answer

Log in to vote
0
Answered by 2 years ago

I would assume you are sending too many requests too often, checkout the data store limits: Roblox Data Store limits

Ad

Answer this question