I have a datastore script for a shop, insteaad of having to manually name and give value to all items, I wanna make a loop that saves it and retrieves it, apparently, it hasn't been working so far, any help??


local storage = game:GetService("DataStoreService"):GetDataStore("I_AM_ON_YOUTUBE")
game.Players.PlayerAdded:Connect(function(plr)
	print("Loading")
	local folder = Instance.new("Folder")
	folder.Parent = plr
	folder.Name = "Shop_Stats"
	local a = 0
	local rrr = {}
	local key = plr.UserId
	for _, v in pairs(plr.PlayerGui:WaitForChild("Shop_GUI"):WaitForChild("Shop"):WaitForChild("Upgrades"):GetChildren()) do
		print("Entering shop")
		if v.ClassName == "TextButton" then
			a = a + 1
			local n = Instance.new("IntValue")
			n.Parent = folder
			n.Name = v.Name
			if storage:GetAsync(key) then
			n.Value = storage:GetAsync(key)[a]
			print("Got async data")
			else
				for _, v in pairs(plr.Shop_Stats:GetChildren()) do
					table.insert(rrr, v.Value)
				end
				storage:SetAsync(key, rrr)
			end
		end
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	print("Saving")
	local key = plr.UserId
	local valuesToSave = {}
	local shop = plr.Shop_Stats
	for _, v in pairs(shop:GetChildren()) do
		table.insert(valuesToSave, tostring(v.Value))
	end
	storage:SetAsync(key, valuesToSave)
	print("Saved")
end)

it won't print("Saved")

Everything else it will do, I may be having a bad format in saving and loading, if so, please correct me.

5 comments

0

The server should not be accessing PlayerGui.

0

If you're testing on studio, it will not output. I had this issue but if you publish your game, join it, and have it output for example the players data, it will save it. In otherwords, its not saving because you're in studio, if you join the actual game when its published it should work as expected.

0

I have api services on

0

Do you have HTTP requests enabled?

0

yeah!