How do I reference or modify an instance based on a string?

Hello, I would like to create an inventory of skins of an item using crates in a game, however, I am unable to find an instance from a value, and any solutions I have tried (mainly from the devforum) have not worked. I have attempted to use different string and data table messages but it seems everything I attempt to find does not work. The script breaks at around line 27 and I would like to have a fix for it.
Here is the current code I have (ignore the tweens)

local gui = script.Parent.Parent.Parent.Parent
local ts = game:GetService("TweenService")
local growinfo = TweenInfo.new(2,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0)
local explodeinfo = TweenInfo.new(2,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local coverscreeninfo = TweenInfo.new(2,Enum.EasingStyle.Quart,Enum.EasingDirection.In,0,true,0)

local itemnames = {
	"Red",
	"Orange",
	"Yellow",
	"Green",
	"Blue"
}

local growtween = ts:Create(gui.SkinCrateImage, growinfo, {Size = gui.SCIReference.Size})
local explosiontween = ts:Create(gui.ExplodeImage, explodeinfo, {Size = gui.EXIReference.Size;Rotation = gui.EXIReference.Rotation})
local coverscreentween = ts:Create(gui.CoverScreen, coverscreeninfo, {BackgroundTransparency = 0})

function leftClick()
	growtween:Play()
	wait(0.3)
	explosiontween:Play()
	wait(0.4)
	coverscreentween:Play()
	wait(2)
	local currentitem = itemnames[math.random(#itemnames)]
	local TargetString = tostring(itemnames)
	gui.OwnedSkinGui.TargetString.BackgroundColor3 = Color3.fromRGB(134, 134, 134)
	gui.Example.Text = currentitem
	gui.ExplodeImage.Size = UDim2.new(0, 1, 0, 1)
	gui.SkinCrateImage.Size = UDim2.new(0, 1, 0, 1)
end

script.Parent.MouseButton1Click:Connect(leftClick)

Each item in the list has a corresponding TextButton with the same name. Thank you!

(BTW I know this is possible with a bunch of if statements but I would like to keep the code clean.)

In this function, it seems like you’re indexing/assuming that the TargetString you’re trying to search for is already there, but you have to implement some sort of sanity check first before doing so

What we use is a nifty function called FindFirstChild(), and what this does is that it searches for the name that you’re trying to look for inside the current children, and this can return back the Instance found, or nil

So adding on what we know here:

function leftClick()
	growtween:Play()
	wait(0.3)
	explosiontween:Play()
	wait(0.4)
	coverscreentween:Play()
	wait(2)
	local currentitem = itemnames[math.random(#itemnames)]
	--local TargetString = tostring(itemnames) We actually don't need this surprisingly, cause this is a table in itself is what we're getting
    local FrameCheck = gui.OwnedSkinGui:FindFirstChild(currentitem) -- If you're trying to find the specified name, use the "currentitem" variable

    if FrameCheck then -- Check if the UI Object you're searching for is there, if it is then we can change it!
        FrameCheck.BackgroundColor3 = Color3.fromRGB(134, 134, 134)
    else
        warn("Oops,", currentitem, "was unable to be found!")
    end

	gui.Example.Text = currentitem
	gui.ExplodeImage.Size = UDim2.new(0, 1, 0, 1)
	gui.SkinCrateImage.Size = UDim2.new(0, 1, 0, 1)
end

Your original code on line 27 was actually getting the string of a table, hence why it errored

1 Like

Thank you so much for helping, it works!