How would i save a ObjectValue when the player leaves?

You can write your topic however you want, but you need to answer these questions:

  1. i want to make it so a ObjectValue saves when the player leaves, parented inside the player so i can save their swords

  2. i have 0 experience with datastore

  3. i tried looking at other pages of the dev forum but it didnt work

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

If you don’t have any experience with data storage, it would be better to learn the fundamentals of data storage.

Here are some video links and forums to help you out.

1st of, i recomend you using an array to save the swords (im gonna try to explain later on)

local DataStoreService = game:GetService("DataStoreService") --you get the datastore service
local SwordsDatastore = DataStoreService:GetDataStore("Swords") --You define the datastore here

local DefaultWeapon = script.Parent:FindFirstChild("DefaultSword") --saves and loads the default sword

local function SaveData(Player)

	local Weapons = {} --thats the array/table where you can save the swords in

	for i,v in pairs(Player.Weapons:GetChildren() --this function loops through all the folder, you nee to change the name/location if im wrong
		if i:isA("ObjectValue") then
		table.insert(i, Weapons)
	end
end)
	
	pcall(function()
		SwordsDatastore:SetAsync(Player.UserId, Weapons)
	end)
end

local function PlayerAdded(Player)
	
	local SavedSwords
	
	local success, error = pcall(function()
		SavedSwords = SwordsDatastore:GetAsync(Player.UserId) --:GetAsync Loads the data from the datastore
	end)
	
	if not success then warn(error) end --warns if something went wrong

	local Folder = Instance.new("Folder") --creates a new folder, where you can later insert the saved data
	Folder.Name = "Weapons"
	Folder.Parent = Player
-- the code for that comes here

	if not SavedSwords then --creates default data, you can add a default swords in that for example
		SavedSwords = {DefaultWeapon}
	end
	Player.Stats.Level.Value = SavedSwords[1]
end

local function PlayerRemoving(Player)
	SaveData(Player)
end

for _, Player in ipairs(game.Players:GetPlayers()) do
	PlayerAdded(Player)
end

game.Players.PlayerAdded:Connect(PlayerAdded)

game.Players.PlayerRemoving:Connect(PlayerRemoving)

thats interesting! i found an alternate way to do it tho but ty!

thank you, ill watch these later