How to save tables to datastore?

So how should I save the data tho

If u buy from the shop in the gui u can only get there the ID

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "Inven"
	

	local success,errorMessage = pcall(function()
		local data = Inv:GetAsync(plr.UserId)
		Inventory = data
	end)
	
	if success then
		print("Data successfully loaded!")
	end
	if errorMessage then
		warn(errorMessage)
	end

	game.Players.PlayerRemoving:Connect(function(plr)
		Inv:SetAsync(plr.UserId.. Inventory)
	end)

	game:BindToClose(function()
		for i,plr in pairs(game.Players:GetPlayers()) do
			Inv:SetAsync(plr.UserId.. Inventory)
			
			
		end
	end)

end)

This is the only PlayerAdded() I have

oh wait nvmmm

I see what u mean by that

Alr it works now!!!

TYSM FOR THE HELP

If this is still the script, you should still make some changes, for instance removing outside connections.

The ideal structure would probably be this:

local players = game:GetService("Players")

local function load(player:Player)
end

local function save(player:Player)
end

game:BindToClose(function()
end)

players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)

Also use UpdateAsync instead of SetAsync. It allows you to insert extra code to prevent data loss, and even if you don’t insert this code, it has certain safety features that SetAsync does not.

1 Like

so its all in the server script?
if so, how should i access if the Player buys Skins(IDs) without any remote events

I’m not saying don’t use remotes - just don’t have the PlayerRemoving and BindToClose connections in your PlayerAdded one.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.