Save Data not work, how to fix it?

I create a save data script. It will save player’s weapon and level of weapon but It does not work. I tried it on roblox. where i was wrong ?

1 Like

v[1] = name of the weapon
v[2] = level of weapon
v[3] = parent (inventory or backpack)

is v[3] an instance? you can only save string, number and boolean values. (and tables that contain string,number or boolean values)

you will also want to save on BindToClose() too just incase the server closes.
and it’ll help to have some sort of autosaving feature as well.

when saving on studio its a bit weird. (because you are the server & client) so you’ll have to depend on your autosaving feature to save in studio. (because playerRemoving and BindToClose don’t always run the way you want it to in studio)

EDIT: as a side note the KEY must be a string

tostring(plr.UserId)

Try tostring(plr.UserId) instead, as a data store key is a string value. There are also 2 problems that end up in studio mode. The first problem is that sometimes, in studio, the player get added after the PlayerAdded event is set up, the solution is to loop through the GetPlayers() method after setting the event, to not miss any players

local function onPlayerAdded(plr)
	...
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
for i, plr in ipairs(game.Players:GetPlayers()) do
	onPlayerAdded(plr)
end

Another problem can be that the PlayerRemoving event listener does not finish, and the game shuts down before it finishes, to fix this, you can use game:BindToClose(), and I use coroutine.wrap() to save multiple players data at once

local function saveData(plr)
	...
end

game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
	for i, plr in ipairs(game.Players:GetPlayers()) do
		coroutine.wrap(saveData)(plr)
	end
end)

Also, you should wrap your calls with pcall, otherwise, if the data request errors, it will stop the execution of other code. For more information on saving players data when the server closes, see here.

2 Likes

i saved v[3] is “Inventory” or “Backpack” to put it in backpack or inventory.
I think i will change it by number, 1 for inventory and 2 for backpack
I dont know about BindToClose() can you give more information ? I alway use PlayerRemoving to save data

1 Like

save a String “Inventory” or “Backback” AND make sure you turn UserId into a string.

tostring(plr.UserId)

and BindToClose() will run when the server closes.

game:BindToClose(function()
    saveAllPlayers() --this will have to be wrapped in coroutine.
end)
1 Like

BindToClose functions run when the server closes (unless the server crashes, that is why you should be autosaving as well), in studio, I find that PlayerRemoving doesn’t finish, but does fire for the last player, BindToClose functions also run when the developer shuts down the server, in the case where PlayerRemoving won’t fire

I dont know about BindtoClose and coroutine. I fixed it, Can you check it ?

I saved it ís “Backpack” and “Inventory”. It is a string

Add pcalls around your SetAsync and GetAsync, otherwise, if it errors, it will stop the execution of code, also, as I mentioned, sometimes, in studio, the player get added after the PlayerAdded event gets set up, simply add this line after your PlayerAdded event

for i, plr in ipairs(game.Players:GetPlayers()) do
	onPlayerAdded(plr)
end

Like this ?
pcall(function()
SetAsync/GetAsync
end)

something like that, however, put some error handlers

-- GetAsync
local data
local success, err = pcall(function()
	data = dataStore:GetAsync(PlayerUserId)
end)

if not success and err then
	warn("Cannot save data for reason: "..err)
else
	...
end

-- SetAsync
local success, err = pcall(function()
	dataStore:SetAsync(PlayerUserId, data)
end)
if not success and err then
	warn("Cannot save data for reason: "..err)
end

This is because data store request occasionally error, there are other things to consider, like retries, but this should get you started.

Can you check it again ?

The thing I would add is this after your pcalls

if not success and err then
	warn("Cannot save data for reason: "..err)
	-- or for GetAsync
	warn("Cannot access data for reason: "..err)
end

That way, if it errors, it will tell you the error message

Am I missing anything else?

When saving the inventory, save a dictionary instead, so it never gets mixed up the then numeric order gets mixed around

tab.Name = k.Name
tab.Level = k.Level.Value
tab.DataType = "Backpack" -- or "Inventory"

Other than that, everything seems fine

1 Like

thank you very much. you helped me very much. I will try it, hopefully it works

1 Like

It does not work in roblox but i tried take sword at roblox and play game in roblox studio, it works…

weird, I don’t think it wouldn’t work in roblox but does in studio

i take sword in roblox and out then i play game in roblox studio, it works.
But it not work when i play game in roblox again