Storing a table into datastore doesnt work?

So,
I have been trying to make a script that saves the player’s weapon inventory (NOT THE PLAYER BACKPACK) , and loads it. But, the problem is that my script is not working and i can not find out why, can anyone help me?

note i basically created a folder named Values into the player, and a folder named inventory into this folder, saved guns are put into the inventory folder
here’s my script

local DataStoreService = game:GetService("DataStoreService")

local InventoryStore = DataStoreService:GetDataStore("Inventory")

game:GetService("Players").PlayerAdded:connect(function(plr)

pcall(function()
	local Guns = InventoryStore:GetAsync("User-"..plr.UserId)
	print("finding guns") 
if Guns then
		print("Found save")
		for i,v in pairs(Guns) do
			print("Adding saved guns to inventory")
			local gunfound = game.ReplicatedStorage.Weapons:FindFirstChild(v)
			if gunfound then
				print("Placed a gun in the players inventory")
				gunfound:Clone().Parent = plr.Values.Inventory
				print(gunfound.Name)
			end
		end
		end
end)
end)

game:GetService("Players").PlayerRemoving:connect(function(plr)
	pcall(function()
		print("Began saving")
		local gunsave = {}
		for i,gun in pairs(plr.Values.Inventory:GetChildren()) do
			if gun then
				print("Added gun to save")
				table.insert(gunsave,gun.Name)
			end
		end
		print(unpack(gunsave))
		InventoryStore:SetAsync("User-"..plr.UserId,gunsave)
		print(InventoryStore)
	end)
end)

For readability here on the forum you can put your code inside three backticks ``` and it formats it.

Example:
```
function test()
print( ‘do stuff’ )
end
```

becomes

function test()
    print( 'do stuff' )
end

Edit: thank you for updating the post.

1 Like

Are there any errors output?

You have print statements throughout so which statement does it get to before it stops?

What is the outcome and how does it differ to what you expected?

With things wrapped in pcall you may find it errors and you never knew. You should always read the return values of a pcall and if it has failed, print the message for debugging purposes.

1 Like

this is what the output prints:
player entered:

finding guns

player left:

Began saving
Added gun to save
Default gun

Okay so it seems like it doesn’t get to the point where it actually saves, or it fails at that exact part. You can see that as it doesn’t print the InventoryStore.

So print the error message from the failed pcall as I suggested in the second half of my last reply.

local success, message = pcall( yourfunc )

if not success then
    warn( message )
end
1 Like

weird, i tried but still the same thing, no errors

Ok So you really should have this serverside. A hacker can just change the value and save what ever they want.

The server can clone the gun and parent it to the player.

The player’s local script can handle the work with the gun.

You have a folder on the server storing all the info about what the players have. That info is saved when the player leaves.

If the player leaves the game, then the player doesn’t exist and can’t run code for itself. You want the server to go “Oh that player isn’t connected anymore. I better save their data.”

I think that would save about 50% of your issues or more.

You have some errors in there like 2 ends and 2 end)s but I think that’s just formatting on the forum.

Once you sort those out you can look at sorting the next stuff if there are any problems.

I used all of this basically word for word:

1 Like

In your PlayerAdded Event, don’t call the whole code as a protected call(PCall). Instead, only use PCall for when you’re retrieving the data from DataStore:GetAsync() and return the data in a PCall. This is shown below.

local success, guns = pcall(function() 
   return InventoryStore:GetAsync("User-"..plr.UserId)
end

if success then
   print(unpack(guns)) 
end
1 Like

thank you but this did nothing as well, i just think this is unpatchable

1 Like

One more thing would be great. If you could print something like “Saves!” after this line below which is in the PlayerRemoving Event, it would help out a bit. See if it prints or not.

1 Like

i did, but it just does not print it, wich means its not even saving, and why? i dont know

someone just told me this script does not work “anymore” did roblox just update data stores and i dont know?

When a server shuts down, its priority is shutting down, therefore the PlayerRemoving event listener is never called/it doesn’t finish.

1 Like

wich means its not working because the server closes before it can set the async?

Pretty much yes. Also stop using SetAsync() to save player data

1 Like

oml thank you so much, i started a local test with two players, and its working.

1 Like