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

DataStore request always adds to queue on PlayerRemoving?

Asked by
0msh 333 Moderation Voter
4 years ago

the title says it all. when I test in studio, my data always adds to queue when I stop the test. what could be the problem?

serv = game:GetService("DataStoreService")
local Stats = serv:GetDataStore("statsdatastoresaves")
local Tools = serv:GetDataStore("toolsv1")
local Properties = game.Workspace:FindFirstChild("GAME_PROPERTIES")

LoadData = function(plr,level)

    pcall(function()
        local GetData = Stats:GetAsync(plr.UserId)
        if GetData then
            level.Value = GetData[1]
        else
            level.Value = tonumber(nil)
        end

        local ActualTools = {}
        local ToolStorage = game.ServerStorage.ITEMS
        if (Tools:GetAsync(plr.UserId)) then
            for _,v in pairs(Tools:GetAsync(plr.UserId)) do
                print(v)
                if ToolStorage:FindFirstChild(v) then
                    table.insert(ActualTools, v)
                end
            end
            for _,v in pairs(ActualTools) do
                ToolStorage:FindFirstChild(v):Clone().Parent = plr:WaitForChild("StarterGear")
            end
        end
    end)
end

SaveData = function(plr)

    pcall(function()
        local lstats = plr:FindFirstChild("saves")
        if (lstats~=nil) then
            local level = lstats:FindFirstChild("level")
            if (level~=nil) then
                Stats:SetAsync(plr.UserId,{level.Value})
            end
        end

        local ActualTools = {}
        local SG = plr:WaitForChild("StarterGear")
        for _,v in pairs(SG:GetChildren()) do
            table.insert(ActualTools, v.Name)
        end
        if ActualTools[1] then
            Tools:SetAsync(plr.UserId,ActualTools)
            warn("loading tool save")
        end
    end)
end

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

    local CanAutoSave = true
    local lstats = Instance.new("Folder",plr)
    lstats.Name = "saves"


    local level = Instance.new("IntValue",lstats)
    level.Name = "save"


    wait()

    pcall(function()
        LoadData(plr,level)
    end)

    local AutomaticSaving = true

    if AutomaticSaving then
        while wait(90) do
            if (plr~=nil) then
                pcall(function()
                    SaveData(plr)
                end)
            else break
            end
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    pcall(function()
            SaveData(plr)
    end)
end)

local ServerTimeout = 5
game:BindToClose(function()
    for _,v in pairs(game.Players:GetPlayers()) do
        if (v~=nil) then SaveData(v) end
    end
    wait(ServerTimeout)
end)

Answer this question