Hello guys.
my datastore system dont save, i tried to fix it but it didn’t work and i dont know what to do.
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("InventorySaving")
game:GetService("Players").PlayerAdded:Connect(function(player)
local data
local inventory = Instance.new("Folder", player)
inventory.Name = "Inventory"
local userId = player.UserId
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if(success) then
if(data ~= nil) then
for name, value in pairs(data) do
local material = Instance.new("IntValue", inventory)
material.Name = name
material.Value = value
end
end
elseif(err) then
warn(err)
end
end)
game:GetService("Players").PlayerRemoving:Connect(function(player)
local inventory = player.Inventory
local userId = player.UserId
local inv = {}
for i,child in pairs(inventory:GetChildren()) do
inv[child.Name] = child.Value
end
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, inv)
end)
if(success) then
elseif(err) then
end
end)
Are you only testing it in studio? and have you tried printing anything in your last statements?
game.Players.PlayerRemoving:Connect(function(player)
print("running")
--code goes here
if(success) then
print("success")
elseif(err) then
print(err)
end
Sometimes PlayerRemoving does not fire in studio. Perhaps you could start a 2 player local server from the Test tab and only leave the game from 1 client. The other client will prevent the game from shutting down and you can check the output on the server instance(The first one that opens when testing in local server).
PS: Most of the time, this isn’t an issue in actual Roblox servers, but if you want to safeguard against it you can use game:BindToClose(func) which runs before the server shuts down.