Weapon save why this is not working

``
local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“Tools”)

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

local inventory = Instance.new("Folder")
inventory.Name = "Inventory"
inventory.Parent = plr

local tools = Instance.new("Folder")
tools.Name = "Tools"
tools.Parent = inventory

local weaponsData
 
pcall(function()
	weaponsData = myDataStore:GetAsync(plr.UserId.."-Weps")
end)

if weaponsData then
	
	for _,weapon in pairs(weaponsData) do
		if game.ServerStorage:FindFirstChild(weapon) then
			local weaponClone = game.ServerStorage.Tools[weapon]:Clone()
			weaponClone.Parent = plr.Inventory.Tools
			print(weapon.." loaded in!")
		end
	end
	
	
end

end)

game.Players.PlayerRemoving:connect(function(plr)

pcall(function()
	local weapons = plr.Inventory.Tools:GetChildren()
	local weaponsTable = {}
	
	for _,v in pairs(weapons) do
		table.insert(weaponsTable,v.Name)
	end
	
	myDataStore:SetAsync(plr.UserId.."-Weps",weaponsTable)
end)

print("Saved weapons")

end)
``

Saving ValueObjects directly under the player instance itself is proven unreliable since when the player leaves objects inside of it commonly get removed before it can be saved.

Better practice is either saving it somewhere on the server or scrapping relying on ValueObjects all-together and using a module/table for saving data.

Try restructuring your data saving system or perhaps using DataStore2 if you don’t know how to.
Good luck

I also misunderstood his question, He isn’t saving the object directly also “datastore2” has proven unreliable.

I believe the error is his quoting.

Sorry, I meant he’s saving ValueObjects directly stored inside the player instance itself, I edited my response.

Edit:
I emphasized in my answer that he should use a table/module system but if he didn’t know how to then DataStore2 would be a start and would at least work over his current system.

I don’t see any errors in his coding so that’s why I suggested restructuring.

1 Like