Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't my script save data to a table?

I'm making an advanced purchase script, but it isn't working. Instead of having to painfully type in a ton of numbers, you could just set the ID of an item and it would take that and put it in a table.

--Give cash on payment
--written 6/24/2014 by ChipioIndustries for Tycoon Kit
--copyright 2014 Chipio Industries, L.L.C.
wait(1)
local ids={}
local items=script.Parent.Frame:GetChildren()
for i=1,#items do
	ids[i].id=items[i].ID.Value              --this line has an error.
	ids[i].value=items[i].Name:sub(1,#items[i]-1)
end
local MarketplaceService = Game:GetService("MarketplaceService")
local plr=script.Parent.Parent.Parent.Parent.Parent
 
MarketplaceService.ProcessReceipt = function(receiptInfo)
	if receiptInfo.PlayerId==script.Parent.Parent.Parent.Parent.Parent.userId then
		for i=1,#ids do
			if receiptInfo.ProductId==ids[i].id then
				plr.leaderstats.Cash.Value=plr.leaderstats.Cash.Value+ids[i].value
				break
			end
		end
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted		
end

Here's the error: attempt to index field '?' (a nil value) And this is what the Explorer looks like:

Player
	PlayerGui
		Menu
			Menu2
				Open
				Buy Cash
					Open
					Purchase       --script is here.
					Frame
						100K
							ID
							Script
						10K
							ID
							Script
						15K
							ID
							Script
						20K
							ID
							Script
						25K
							ID
							Script
						50K
							ID
							Script
						5K
							ID
							Script
						75K
							ID
							Script

Any ideas on how to fix this?

1 answer

✓ Accepted answer

Yes. The problem is that you're trying to set id in and value in something that doesn't exist. When you're going through all the values in items, you are indexing that number in ids which is nil because the table is empty, and then you try to set the id and value of nil which you can't do.

To fix this you can replace your loop with this:

for i=1,#items do
    ids[i]={}
    ids[i].id=items[i].ID.Value
    ids[i].value=items[i].Name:sub(1,#items[i]-1)
end

This fixes it by setting a the nil value to a table, so now, instead of setting id & value in nil, you're setting id&value in a table.

I hope I explained this good. I'm bad at explaining (if you couldn't tell.), the code should work anyway. Please give me a point or whatever they use here to show people that this is answered (if it worked).

2 comments

0

That didn't work.

0

It fixed that part, but now it doesn't return any errors.

This discussion is preserved as it was. New answers, comments, and votes are closed.