#datastore
1 messages · Page 1 of 1 (latest)
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
I would recommend you to use ProfileService.
Because this code is lacking some important things
okay ill look into that
what kinda tihngs
ProfileService IS STABLE, BUT NO LONGER SUPPORTED - USE “ProfileStore” FOR NEW PROJECTS - Click for more info Mad Studio cross-promo: Check out ReplicaService for state replication / networking! If you’re curious about how Madwork is written, give MadLife - code_guidebook a read! Consider donating R$ to the creator of ProfileService ...
default data changing, server shutdown, maybe something else
your server shutdown approach is wrong
also I recommend using local functions, they are faster
ok
use profilestore its most recent one
ok
** You are now Level 3! **