Datastore never ever saves

Okay so im trying to make a datastore for my game but its never saving, i have everything i need for a working datastore it just wont save heres my script

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore1")

game.Players.PlayerAdded:Connect(function(plr)
	
	local folder = Instance.new("Folder", plr)
	folder.Name = "Inventory"
	
	local success, errormsg = pcall(function()
		local loaded = DataStore:GetAsync(plr.UserId)
		
		if loaded then
			for i, v in pairs(loaded)do
				local x = Instance.new("IntValue", folder)
				x.Name = v
			end
		end
	end)
	
end)

function save(plr)
	
	local saveTable = {}
	
	for i, v in pairs(plr:WaitForChild("Inventory"):GetChildren())do
		saveTable[#saveTable + 1] = v.Name
	end
	
	DataStore:UpdateAsync(plr.UserId, saveTable)
	
end

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then
		for i, plr in pairs(game.Players:GetChildren())do
			save(plr)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	if not game:GetService("RunService"):IsStudio() then
		save(plr)
	end
end)
1 Like

Those might be the problems

  • First, if the player is new, there won’t be any new “IntValue” because the loaded variable will then be nil.
  • Second, you didn’t load the value of the ones you saved.

okay ill try using that then give me a moment

1 Like