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

I keep getting an error when I leave my game, why is this?

Asked by
CodeWon 181
3 years ago

I tested my datastore for the 1st time today and I got an error when I leave the game: "DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = Player_1251627904" Why do I keep getting this even though I tested this only once.

This is where data is loaded:

local DataStoreService = game:GetService("DataStoreService")
local gameData = DataStoreService:GetDataStore("gameData")

game.Players.PlayerAdded:Connect(function(player)

    -- Leaderstats

    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local Bucks = Instance.new("IntValue",leaderstats)
    Bucks.Name = "Bucks"

    local Level = Instance.new("IntValue",leaderstats)
    Level.Name = "Level"

    -- Data

    local key = "Player_"..player.UserId -- The key is where the data is stored 

    local data
    local succes, errorMessage = pcall(function()
        data = gameData:GetAsync(key) -- gameData:GetAsync(key) finds the data stored under the players key
    end)

    if succes then
        -- If the pcall above is successful, the players data will be loaded
        Bucks.Value = data["Bucks"]
        Level.Value = data["Level"]
        print("Data loaded for "..player.Name.."/"..key)
    end

    if data == nil then
        -- If the player has no data, then the code below will run
        print(player.Name.."/"..key.." is a new player and has no data")
        player.leaderstats.Bucks.Value = 50
        player.leaderstats.Level.Value = 1
    end
end)

This is where the data is saved:

function saveData(plr) -- This will be called when player leaves

    local key = "Player_"..plr.UserId

    local data = { 
        -- Everything in this table is what will be saved
        Bucks = plr.leaderstats.Bucks.Value;
        Level = plr.leaderstats.Level.Value;
    }

    local success, errorMessage = pcall(function()
        gameData:SetAsync(key, data) -- gameData:SetAsync(key, data) sets the data under the players key to the current data
    end)

    if success then
        print("Saved data for "..plr.Name.."/"..key)
    else
        print("Error saving data for "..plr.Name.."/"..key)
        warn("Error message: "..errorMessage)
    end
end

game.Players.PlayerRemoving:Connect(function(player)
    saveData(player)
end)

game:BindToClose(function()
    -- game:BindToClose() detects when the server is shutdown, the code below will run on server shutdown
    -- If the server shutsdown, all the player's data needs to be saved
    for _, player in pairs(game.Players:GetChildren()) do
        saveData(player)
    end
end)

I really hope there is a way to fix this, becaise its annoying and I'm not sending too many requests, I only played once

Edit: The level leaderstats do not save or load when joining/leaving the game, can someone help me with this too?

Answer this question