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

Why are my table values turning to nil?

So I have this table here:

local GodlyItems = {
	Items.SL;
	Items["Night Eye"]
}

So I try running this:

while rerun==true do
	rerun = false
	arghh = math.random(1,1)
	if arghh == 1 then
		randomnumber = math.random(1,godlyamount)
		local thonk = GodlyItems[randomnumber]
		print("[GODLY!] "..thonk.Name.." was the selected item!")
		ItemName.Text = ("[GODLY!] "..GodlyItems[randomnumber].Name.." - ".. GodlyCosts[randomnumber].." Coins")
		ItemName.Text = (GodlyItems[randomnumber].Name.." - ".. GodlyCosts[randomnumber].." Coins")
		ItemImage.Image = ("rbxassetid://"..GodlyItemImages[randomnumber])
		BuyButton.Visible = true
		i = GodlyItemTimes[randomnumber]
	else

... continued

But for some reason, I get this error:

20:13:54.079 - Players.StolenLimited.PlayerGui.Guis.LocalScript:136: attempt to index local 'thonk' (a nil value)

20:13:54.081 - Stack Begin

20:13:54.082 - Script 'Players.StolenLimited.PlayerGui.Guis.LocalScript', Line 136

20:13:54.083 - Stack End

Line 136 is this:

print("[GODLY!] "..thonk.Name.." was the selected item!")

Can anyone figure out why it's nil?

1 answer

If you have set godlyamount to a number greater than #GodlyItems (2 that you've shown us) then math.random could definitely be returning a value that doesn't exist. That's why you usually see something like local item = items[math.random(1, #items)] -- that should never fail, so long as items has 1+ items in it.

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