Table.find using variables in a table not working

Hey im trying to use table find with variables that contains image id but always says
Unable to assign property Image. Content expected, got nil
printing the table and the searched element works tho

Here’s the script

local BallsImagesId = {

	BlackBall = {Id = "rbxassetid://18607983731"},
	BlueBall  = {Id = "rbxassetid://18608189104"},
	CyanBall  = {Id = "rbxassetid://18607996283"},
	EyeBall  = {Id = "rbxassetid://18607997899"},
	FireBall  = {Id = "rbxassetid://18607999697"},
	GreenBall  = {Id = "rbxassetid://18608000867"},
	HackerBall  = {Id = "rbxassetid://18608002663"},
	HeavenBall  = {Id =  "rbxassetid://18608004352"},
	MagmaBall  = {Id =  "rbxassetid://18608005611"},
	RainbowBall  = {Id =  "rbxassetid://18608006563"},
	RedBall  = {Id =  "rbxassetid://18608007621"},
	StudsBall  = {Id =  "rbxassetid://18608008844"},
	YellowBall  = {Id =  "rbxassetid://18608010555"},
	BlackHoleBall  = {Id =  "rbxassetid://18608111624"}
}

local ballsTable = {"BlackBall", "BlueBall", "CyanBall", "EyeBall", "FireBall", "GreenBall", "HackerBall", "HeavenBall", "MagmaBall", "RainbowBall", "RedBall", "StudsBall", "YellowBall", "BlackHoleBall"}

Button.MouseButton1Click:Connect(function()

local BoughtBall = ballsTable[math.random(1, #ballsTable)]
 Image.Image = table.find(BallsImagesId, BoughtBall)

end)

1 Like
local BallsImagesId = {

	BlackBall = {Id = "rbxassetid://18607983731"},
	BlueBall  = {Id = "rbxassetid://18608189104"},
	CyanBall  = {Id = "rbxassetid://18607996283"},
	EyeBall  = {Id = "rbxassetid://18607997899"},
	FireBall  = {Id = "rbxassetid://18607999697"},
	GreenBall  = {Id = "rbxassetid://18608000867"},
	HackerBall  = {Id = "rbxassetid://18608002663"},
	HeavenBall  = {Id =  "rbxassetid://18608004352"},
	MagmaBall  = {Id =  "rbxassetid://18608005611"},
	RainbowBall  = {Id =  "rbxassetid://18608006563"},
	RedBall  = {Id =  "rbxassetid://18608007621"},
	StudsBall  = {Id =  "rbxassetid://18608008844"},
	YellowBall  = {Id =  "rbxassetid://18608010555"},
	BlackHoleBall  = {Id =  "rbxassetid://18608111624"}
}

local ballsTable = {"BlackBall", "BlueBall", "CyanBall", "EyeBall", "FireBall", "GreenBall", "HackerBall", "HeavenBall", "MagmaBall", "RainbowBall", "RedBall", "StudsBall", "YellowBall", "BlackHoleBall"}


local BoughtBall = ballsTable[math.random(1, #ballsTable)]
print(BallsImagesId[BoughtBall])

This should hopefully work, it returns a table, you just need to retrieve “Id” from it.

Basically, instead of using table.find you just get the object in the index from the randomly generated number.

Full code for your purpose:


	BlackBall = {Id = "rbxassetid://18607983731"},
	BlueBall  = {Id = "rbxassetid://18608189104"},
	CyanBall  = {Id = "rbxassetid://18607996283"},
	EyeBall  = {Id = "rbxassetid://18607997899"},
	FireBall  = {Id = "rbxassetid://18607999697"},
	GreenBall  = {Id = "rbxassetid://18608000867"},
	HackerBall  = {Id = "rbxassetid://18608002663"},
	HeavenBall  = {Id =  "rbxassetid://18608004352"},
	MagmaBall  = {Id =  "rbxassetid://18608005611"},
	RainbowBall  = {Id =  "rbxassetid://18608006563"},
	RedBall  = {Id =  "rbxassetid://18608007621"},
	StudsBall  = {Id =  "rbxassetid://18608008844"},
	YellowBall  = {Id =  "rbxassetid://18608010555"},
	BlackHoleBall  = {Id =  "rbxassetid://18608111624"}
}

local ballsTable = {"BlackBall", "BlueBall", "CyanBall", "EyeBall", "FireBall", "GreenBall", "HackerBall", "HeavenBall", "MagmaBall", "RainbowBall", "RedBall", "StudsBall", "YellowBall", "BlackHoleBall"}

Button.MouseButton1Click:Connect(function()

	local BoughtBall = ballsTable[math.random(1, #ballsTable)]
	Image.Image = BoughtBall["Id"]

end)```
1 Like

table.find() only works for Arrays as opposed to Dictionaries, which if you’re wondering the difference, just pay attention to whether or not its using a string to represent an item.

a = {"this", "that"} -- an Array, you can use table.find()
-- index is represented a number
b = {This = 1, That = 2} -- a Dictionary, you cant use table.find()
-- index is represented as a string
a[1] -- Array
b.This -- Dictionary
table.find(a, "this") -- Array

So with your list of strings, use them to check whether the Index actually exists, and then apply the relevant data, for example:

if BallsImagesId[BoughtBall] then -- to ensure that the item exists
-- code
local Asset = BallsImagesId[BoughtBall] -- grab whatever data
Image.Image = Asset.Id -- apply whatever data

If you want to convert to an Array, you can do this:

{
    {"BlackBall",  "rbxassetid://18608010555"},
    -- whatever else there is
}

However it is more convinient to use a dictionary.

2 Likes

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