Is there a more efficient way to do this?

Sorry if this is way to long to read but if you have the time could you please check this out and tell me if there is any way to make this take up less of the script because it’s so looooong and seems so tedious. Any replies are appreciated!

if object2.Name == "Melee" or object2.Name == "Melee+" then
						if object2.Name == "Melee+" then
							Plus = true
						end
						Image = SidesGUI.Melee.ImageLabel:Clone()
						Text = SidesGUI.Melee.TextLabel:Clone()
						if object2.Value ~= nil then
							if Plus == true then
								Text.Text = tostring(object2.Value) .. "+"
								Card.SurfaceGui.DieSides:FindFirstChild("Side" .. tostring(object2.Side.Value)).BackgroundColor3 = Color3.fromRGB(36, 110, 189)
							else
								Text.Text = tostring(object2.Value)
							end
						end
						Plus = false
					elseif object2.Name == "Ranged" or object2.Name == "Ranged+" then
						if object2.Name == "Ranged+" then
							Plus = true
						end
						Image = SidesGUI.Ranged.ImageLabel:Clone()
						Text = SidesGUI.Ranged.TextLabel:Clone()
						if object2.Value ~= nil then
							if Plus == true then
								Text.Text = tostring(object2.Value) .. "+"
								Card.SurfaceGui.DieSides:FindFirstChild("Side" .. tostring(object2.Side.Value)).BackgroundColor3 = Color3.fromRGB(36, 110, 189)
							else
								Text.Text = tostring(object2.Value)
							end
						end
						Plus = false
					elseif object2.Name == "Indirect" or object2.Name == "Indirect+" then
						if object2.Name == "Indirect+" then
							Plus = true
						end
						Image = SidesGUI.Indirect.ImageLabel:Clone()
						Text = SidesGUI.Indirect.TextLabel:Clone()
						if object2.Value ~= nil then
							if Plus == true then
								Text.Text = tostring(object2.Value) .. "+"
								Card.SurfaceGui.DieSides:FindFirstChild("Side" .. tostring(object2.Side.Value)).BackgroundColor3 = Color3.fromRGB(36, 110, 189)
							else
								Text.Text = tostring(object2.Value)
							end
						end
						Plus = false
					elseif object2.Name == "ChangeSide" then
						Image = SidesGUI.ChangeSide.ImageLabel:Clone()
						Text = SidesGUI.ChangeSide.TextLabel:Clone()
						if object2.Value ~= nil then
							Text.Text = tostring(object2.Value) 
						end
					elseif object2.Name == "Resource" then
						Image = SidesGUI.Resource.ImageLabel:Clone()
						Text = SidesGUI.Resource.TextLabel:Clone()
						if object2.Value ~= nil then
							Text.Text = tostring(object2.Value) 
						end
					elseif object2.Name == "Ability" then
						Image = SidesGUI.Ability.ImageLabel:Clone()
						Text = SidesGUI.Ability.TextLabel:Clone()
						if object2.Value ~= nil then
							Text.Text = tostring(object2.Value) 
						end
					elseif object2.Name == "Shield" then
						Image = SidesGUI.Shield.ImageLabel:Clone()
						Text = SidesGUI.Shield.TextLabel:Clone()
						if object2.Value ~= nil then
							Text.Text = tostring(object2.Value) 
						end
					elseif object2.Name == "Disrupt" then
						Image = SidesGUI.Disrupt.ImageLabel:Clone()
						Text = SidesGUI.Disrupt.TextLabel:Clone()
						if object2.Value ~= nil then
							Text.Text = tostring(object2.Value) 
						end
					elseif object2.Name == "Blank" then
						Image = SidesGUI.Blank.ImageLabel:Clone()
						Text = SidesGUI.Blank.TextLabel:Clone()
						if object2.Value ~= nil then
							Text.Text = tostring(object2.Value) 
						end
					elseif object2.Name == "Discard" then
						Image = SidesGUI.Discard.ImageLabel:Clone()
						Text = SidesGUI.Discard.TextLabel:Clone()
						if object2.Value ~= nil then
							Text.Text = tostring(object2.Value) 
						end
					end

Sorry about the horrible indents, it happens when I copy code sometimes.

Could do something like

Image = SidesGUI:FindFirstChild(object2.Name).ImageLabel:Clone()
Text = SidesGUI:FindFirstChild(object2.Name).TextLabel:Clone()
if object2.Value ~= nil then
    Text.Text = tostring(object2.Value) 
end

and just have it as 1 thing

The whole thing can be replaced with

-- nil if plus isn't found
local plusIndex = string.find(object2.Name, "%+")

-- whole word if plusIndex is nil
local name = string.sub(object2.Name, 1, plusIndex)

Image = SidesGUI[name].ImageLabel:Clone()
Text = SidesGUI[name].TextLabel:Clone()

if object2.Value then
	if plusIndex then
		Text.Text = tostring(object2.Value) .. "+"
		Card.SurfaceGui.DieSides["Side" .. tostring(object2.Side.Value)].BackgroundColor3 = Color3.fromRGB(36, 110, 189)
	else
		Text.Text = tostring(object2.Value)
	end
end

I would also suggest you look into [All about Object Oriented Programming](Object-Oriented Programming) for problems like this. It may help you organize your code a bit more. But not necessary, just a suggestion.

1 Like

There is a prblem with this though, there is no item that has a plus in SidesGUI. I was manually adding the plus to the text with the script. Would I just have to make an if statement like this before Image and Text are defined?

if plusIndex then
     -- manually add the "+"
end

Sorry if this seems like a dumb question, the code is a little complicated for me.