game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
local Character = Player.Character or Player.CharacterAdded:Wait()
Character:WaitForChild("Humanoid").Seated:Connect(function(IsSeated, WhatSeat)
if IsSeated then
if WhatSeat.Name == "DriveSeat" then
if Player.Backpack:FindFirstChild("M4A1 ") then
Player.Backpack:FindFirstChild("M4A1").Parent = game.ServerStorage:FindFirstChild(Player.Name.."-Trunk")
end
end --Backpack
end
end) --Load2
end)
local Folder = Instance.new("Folder", game.ServerStorage)
Folder.Name = Player.Name.."-Trunk"
local data
local success = pcall(function()
data = Data:GetAsync(Player.UserId.."-Trunk")
end)
if success and data then
print("Hi")
for i,v in pairs(data) do
if v[1] then
local Folder2 = Instance.new("Folder", Folder)
Folder2.Name = v[1]
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local StuffToSave = {}
for i,v in ipairs(game.ServerStorage:FindFirstChild(Player.Name.."-Trunk"):GetChildren()) do
table.insert(StuffToSave, {v.Name})
print("Saved "..v.Name)
end
game.ServerStorage:FindFirstChild(Player.Name.."-Trunk"):Destroy()
if StuffToSave[1] then
print(StuffToSave[1])
end
Data:SetAsync(Player.UserId.."-Trunk", StuffToSave)
end)
For some reason, folders ain’t getting created, like if it wasn’t saving or there’s no data, but here’s no errors, any idea, It’s printing the tool about to get saved, but it’s not priting “Hi”
Also, do you have API access for studio turned on for your game? Print the success of your data call and the error that is returned by the call just to make sure.
If you’re in studio, the server will shut down the moment you leave and it’s likely this will happen in game. Since SetAsync() is a yielding function, it doesn’t complete before the server closes. You need to save the data before the player presses “leave”. Also, print() does not yield so that can complete on closing.
Haven’t had any success with saving data, for reasons I’m still trying to figure out.
Anyhow, the main focus here is retrieving data first, which goes as follows:
local dss = game:GetService('DataStoreService')
local plrs = game:GetService('Players')
local db = dss:GetDataStore('example_db')
local default_data = {
lvl = 1,
exp = 0,
hp = 100,
sp = 5,
save_point = 'area_01',
unlocked_skills = {
1, 2, 3
}
}
local function get_user_key(plr)
return tostring('user_' ..plr.userId)
end
local function get_plr_data(plr)
local success, data = pcall(function()
return db:GetAsync(get_user_key(plr))
end)
if not data or success then
data = default_data
end
if not success then
warn('Unable to retrieve data from the server')
end
return data
end
Instead of running SetAsync() right when the player leaves, try adding an autosave function or something that saves the data when the data itself changes.
Okay, so, as it creates a folder, I will use a .Changed function on folder and save stuff there. and add a debounce for it doesn’t exceeds data slow response