0

I'm doing a simulator in Roblox Studio and I need to save the player's inventory more this error happened. How do I fix this error? I'm looking forward to finishing my Roblox Studio simulator, here's the code.

local Players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Test")
local run = game:GetService("RunService")
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local toolConfig = require(replicatedStorage.Config.ToolConfig)

local function givePlayerCurrency(player: player)
    while true do
        task.wait(1)
        player.leaderstats.Biceps.Value += 1
        player.leaderstats.Triceps.Value += 1
        player.leaderstats.Quads.Value += 1
        player.leaderstats.Pectoral.Value += 1
    end
end

local function giveTool(player: player, tool: string)
    repeat task.wait() until player.Character
    local toolClone = serverStorage.Tools:FindFirstChild(tool):Clone()
    toolClone.Parent = player.Backpack
end

local function WaitForRequestBudget(requestType)
    local currentBudget = dataStoreService:GetRequestBudgetForRequestType(requestType)
    while currentBudget < 1 do
        currentBudget = dataStoreService:GetRequestBudgetForRequestType(requestType)
        task.wait(5)
    end
end
    
local function setupPlayerData(player: player)
    local userID = player.UserId
    local key = "player"..userID
    
    
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    
    local biceps = Instance.new("IntValue", leaderstats)
    biceps.Name = "Biceps"
    biceps.Value = 0
    
    local triceps = Instance.new("IntValue", leaderstats)
    triceps.Name = "Triceps"
    triceps.Value = 0
    
    local quads = Instance.new("IntValue", leaderstats)
    quads.Name = "Quads"
    quads.Value = 0
    
    local back = Instance.new("IntValue", leaderstats)
    back.Name = "Back"
    back.Value = 0
    
    local pectoral = Instance.new("IntValue", leaderstats)
    pectoral.Name = "Pectoral"
    pectoral.Value = 0
    
    local inventoryFolder = Instance.new("Folder", player)
    inventoryFolder.Name = "inventory"
    
    local toolsFolder = Instance.new("Folder", inventoryFolder)
    toolsFolder.Name = "ownedTools"
    
    local equippedTool = Instance.new("StringValue", inventoryFolder)
    equippedTool.Name = "EquippedTool"
    equippedTool.Value = "BagEasy"
    
    for tool, toolTable in pairs(toolConfig) do
        local toolBoolean = Instance.new("BoolValue", toolsFolder)
        toolBoolean.Name = tool
        if tool == "BagEasy" then
            toolBoolean.Value = true
        else
            toolBoolean.Value = false
        end
    end
    
    
    local success, returnValue
    repeat 
        WaitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
        success, returnValue = pcall(dataStore.GetAsync, dataStore, key)
    until success or not Players:FindFirstChild(player.Name)
    
    
    if success then
        if returnValue == nil then
            returnValue = {
                Biceps = 0,
                Triceps = 0,
                Quads = 0,
                Pectoral = 0,
            }
        end
        
        print(returnValue)
        player.leaderstats.Biceps.Value = if returnValue.Biceps ~= nil then returnValue.Biceps else 0
        player.leaderstats.Triceps.Value = if returnValue.Triceps ~= nil then returnValue.Triceps else 0
        player.leaderstats.Quads.Value = if returnValue.Quads ~= nil then returnValue.Quads else 0
        player.leaderstats.Pectoral.Value = if returnValue.Pectoral ~= nil then returnValue.Pectoral else 0
        player.inventory.EquippedTool.Value = if returnValue.Inventory.EquippedTool ~= nil then returnValue.Inventory.EquippedTool else "BagEasy"
        
        for _, tool in ipairs(player.inventory.ownedTools:GetChildren()) do
            print(tool.Name)
            tool.Value = if returnValue.Inventory.ownedTools[tool.Name] ~= nil then returnValue.Inventory.ownedTools[tool.Name] else false
        end
    else
        player:Kick("Data store dont working error")
        print(player.Name.."DataStore error unloaded")
    end
    
    giveTool(player, "BagEasy")
    --givePlayerCurrency(player)
end

local function save(player)
    local userID = player.UserId
    local key = "player"..userID
    
    local biceps = player.leaderstats.Biceps.value
    local triceps = player.leaderstats.Triceps.value
    local quads = player.leaderstats.Quads.value
    local pectoral = player.leaderstats.Pectoral.value
    local equippedTool = player.inventory.EquippedTool.Value
    
    local ownedToolTable = {}
    
    for _, tool in ipairs(player.inventory.ownedTools:GetChildren()) do
        ownedToolTable[tool.Name] = tool.Value
    end
        
    
    local dataTable = {
        Biceps = biceps,
        Triceps = triceps,
        Quads = quads,
        Pectoral = pectoral,
        Inventory = {
            EquippedTools = equippedTool,
            OwnedTools = ownedToolTable,
        }
    }
    print(dataTable)
    local success, returnValue
    repeat
        WaitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
        success, returnValue = pcall(dataStore.UpdateAsync, dataStore, key, function()
            return dataTable
        end)
    until success
    
    if success then
        print("data save")
    else
        print("Data save    ERROR")
    end
    
end

local function onShutdown()
    if run:IsStudio() then
        task.wait(2)
    else
        local finished = Instance.new("BindableEvent")
        local allPlayers = Players:GetPlayers()
        local leftPlayer = #allPlayers
        
        for _, player in ipairs(allPlayers) do
            coroutine.wrap(function()
                save(player)
                leftPlayer -= 1
                if leftPlayer == 0 then
                    finished:Fire()
                end
            end)()
        end
        finished.Event:Wait()
    end
end

for _, player in ipairs(Players:GetPlayers()) do
    coroutine.wrap(setupPlayerData())(player)
end

Players.PlayerAdded:Connect(setupPlayerData)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)

while true do
    task.wait(600)
    for _, player in ipairs(Players:GetPlayers()) do
        coroutine.wrap(save)(player)
    end
end

I'm trying to make a system to save the inventory of the player I expect answers.

Dharman
  • 30,962
  • 25
  • 85
  • 135

0 Answers0