#datastore

1 messages · Page 1 of 1 (latest)

rustic token
#

this shuld be pretty easy im just new and need sum1 to double check since this is important 😎

#
local DataStoreService = game:GetService("DataStoreService")
local InvData = DataStoreService:GetDataStore("InventoryData")
local InvTable = require(game:GetService("ServerScriptService").InvTable)
local Players = game:GetService("Players")
game:GetService("Players").PlayerAdded:Connect(function(player)
    local attempt = 0
    repeat 
        attempt += 1
        local success, result = pcall(function()
            return InvData:GetAsync(player.UserId)
        end)
            if success then
            InvTable.CurrentPlayers[player.UserId] = result or {
                    Items = {},
                    Coins = 0,
                }
            player:WaitForChild("leaderstats"):WaitForChild("Coins").Value = InvTable.CurrentPlayers[player.UserId].Coins
            else
                warn(result)
                task.wait(3)
            end
    until success or attempt == 5
    if attempt == 5 then
        player:Kick("Could not load data. Please try again later.")
    end
end)
#
function PlayerDisconnect(player)
    if InvTable.CurrentPlayers[player.UserId] then
        local attempt = 0
        repeat
            attempt += 1
            local success, result = pcall(function()
                InvData:SetAsync(player.UserId, InvTable.CurrentPlayers[player.UserId])
            end)
            if success then
                print("Saved data.",player.Name)
            else
                warn(result)
                task.wait(3)
            end
        until success or attempt == 5
        if attempt == 5 then
            warn("Failed to save data.",player.Name)
        end
    else
        warn("Inventory or DataStore does not exist.")
    end
end
function ServerShutdown()
    for _, v in ipairs(Players:GetPlayers()) do
        task.spawn(function()
            PlayerDisconnect(v)
        end)
    end
end
game:GetService("Players").PlayerRemoving:Connect(PlayerDisconnect)
game:BindToClose(ServerShutdown)
#

all in da same script obv

#

heres invtable script

local InvTable = {}
InvTable.CurrentPlayers = {}

return InvTable
stray pecan
#

I would recommend you to use ProfileService.

#

Because this code is lacking some important things

rustic token
#

okay ill look into that

rustic token
stray pecan
#
stray pecan
#

your server shutdown approach is wrong

#

also I recommend using local functions, they are faster

rustic token
#

ok

analog prairie
rustic token
#

ok

exotic stoneBOT
#

studio** You are now Level 3! **studio