#saving works in studio but not in game..?

1 messages · Page 1 of 1 (latest)

charred surge
#
    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 food = Instance.new("IntValue", leaderstats)
    food.Name = "Food"
    food.Value = 0
    
    local coins = Instance.new("IntValue", leaderstats)
    coins.Name = "Coins"
    coins.Value = 0
    
    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 = {
                Food = 0,
                Coins = 0,
            }
        end
        
        print(returnValue)
        food.Value = if returnValue.Food ~= nil then returnValue.Food else 0
        coins.Value = if returnValue.Coins ~= nil then returnValue.Coins else 0
    
    else
        player:Kick("There was an error loading your Data! Roblox's DataStore might be down, try again later, or contact us through our Discord!")
        print(player.Name.." Data loading ERROR!!!")
    end
    
    givePlayerCurrency(player)
end

local function save(player)
    local userID = player.UserId
    local key = "Player_"..userID
    
    local food = player.leaderstats.Food.Value
    local coins = player.leaderstats.Coins.Value
    
    local dataTable = {
        Food = food,
        Coins = coins,
    }
    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 Saved!")
        
    else
        print("Data Saving ERROR!!!")
    end
end


local function onShutdown()
    if RunService:IsStudio() then
        task.wait(2)
    else
        local finished = Instance.new("BindableEvent")
        local allPlayers = Players:GetPlayers()
        local leftPlayers = #allPlayers
        
        for _, player in ipairs(allPlayers) do
            coroutine.wrap(function()
                save(player)
                leftPlayers -= 1
                if leftPlayers == 0 then
                    finished:Fire()
                end
            end)()
        end
        finished.Event:Wait(10)
    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```
#

it should save in game

#

but idk why its not

charred surge
#

omg

near otterBOT
#

studio** You are now Level 1! **studio

charred surge
#

i just realised i never published to roblox

#

😭

analog cypress