IntValue Not Being Created

Hello,
I just made a database script for my hat saving, however, the IntValue doesn’t get created for some reason. The data does get save when you leave but the only problem is creating the IntValue and changing the name into v.Name and putting into the player folder.

My script below:

	pcall(function()
		local hats = dataStore:GetAsync(key)
		
		if hats then
			for i,v in pairs(hats) do
				if v then
					local restoreHat = Instance.new("IntValue")

					restoreHat.Name = v.Name
					restoreHat.Parent = plr:WaitForChild("Infos").Hats
				end
			end
			hatEquip:FireClient(plr, "refreshInventory")
		end
	end)

Much appreciated.

local success, err = pcall(function()
	local hats = dataStore:GetAsync(key)
	print(`Store:`, hats)
	
	if hats then
		for i, v in pairs(hats) do
			print(`Object:`, i, v)
			if v then
				local restoreHat = Instance.new("IntValue")
				restoreHat.Name = v.Name
				restoreHat.Parent = plr:WaitForChild("Infos").Hats
			end
		end
		hatEquip:FireClient(plr, "refreshInventory")
	end
end)

if (not success) then
	warn(`This is your issue: {err}`)
end

Let me know what is print, like sharing a screenshot maybe.

Looks like your code is in a pcall, this swallows errors so you might want to remove the pcall temporarily to figure out the issue with the code block. Then you can place it back.

One thing I notice about your code is that you interact with hats as if it was a table, but I wouldn’t recommend storing them directly, I would use HttpService:JSONEncode() to turn it into a string, saving it, and then when retrieving the data, using HttpService:JSONDecode() to turn it back into a table.

image

also labeled them for you

	if hats then
		print(hats) --print the table of the hat
		for i,v in pairs(hats) do
			if v then
				print(v) --the name of the hat
				
				local restoreHat = Instance.new("IntValue")

				restoreHat.Name = v.Name
				restoreHat.Parent = plr:WaitForChild("Infos").Hats
				
				print(restoreHat.Parent) --nothing or niled
			end
		end
		hatEquip:FireClient(plr, "refreshInventory")
	end

It states “Unable to assign property Name. string expected, got nil”, I’m not sure why. It seems to print out perfectly. I’ve provided the print above to determine if I need to use JSON.

You do not need to use JSON, roblox can natively save table in the data store from a while. Using JSON is an old practice to save table from since a time when this was not possible.

But your issue is pretty simple, v is the value of the table not an Instance, it’s a string.

restoreHat.Name = v -- That's all

Oh, I just realized that too. I’m dumb for overcomplicating things. Thank you so much!

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