Data not saving

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”
image

Copied the exact same code into my place, once I rejoin all the folders are there. Are you sure you were checking for folders from server side?

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.

Yes I did. 30 charssssss, I m checking folders from server sided, and it’s not showing on trunk

Yes it’s turned on, What d you mean printing success?? 30chars

It’s still not working for some reason

Hi!

Are you sure the player has data when retrieving that player’s data from the server?

To make this so, you should first save default data for that player to the server before you attempt to retrieve it.

If you want to avoid this, you could also assign a default table of values to your variable “data” when data is nil or {}

If you don’t know how to do this, I can help you with that further.
But before that, please try it out yourself!

I kinda don’t understand too much, sorry

Can you explain to me better?
(30chars)

1 Like

Yes, I will provide you with a simple example. Give me a bit to write it properly! I haven’t worked with datastorage in a while :sweat_smile:

Thank you so much! 30charsdsss

So…? Any idea? :slight_smile: it’s not working ;(

I’m actually having the same issue myself; using the pcall() function.

Saving data just straight up does not seem to work.
For me, atleast.

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.

Hope this helped! :slightly_smiling_face:

How can I do that? Or can I do a game:BindToClose() function?

Haven’t had any success with saving data, for reasons I’m still trying to figure out. :sleepy:
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

If you were to go with that approach, I recommend requesting to save data from a localscript to the server every X seconds.