Checking for blank or missing int and string values

Hello, I recently got into learning scripting about 3 weeks ago. No prior knowledge of coding.
I was working upon TheDevKing’s vote system script but heavily modifying it.

the votesystem gets 3 random maps from my game script and displays them on a surfaceGUI.
maps are kept in ServerStorage under a “maps” folder.
maps have a intvalue called ImgID and a stringvalue called RealName.

For example lets have a map called Snowy Hill.
the map model will be called snowyhill at ServerStorage.maps.snowyhill
its ImgID.Value at maps.snowyhill.ImgID would equal to “rbxassetid://example”
and its RealName.Value at maps.snowyhill.RealName would equal to “Snowy Hill”

vote buttons are image buttons
choice titles are titles above the vote button
image

here is how the titles show up if RealName is missing or blank
image

Lets say they are missing or not valid.
I would like to make sure they print a message and have a placeholder show up instead of showing up blank.
I wanted to make sure the script doesn’t halt from an error.

This is the code I wrote for it. But I feel like it could be improved on.


local placeHolderImage = "rbxassetid://3037364038"
local placeholderText = ("PH!")

-- update display choice images
local function UpdateDisplayImages()
	print("UpdateDisplayImages function called")
	local bttImgTable = {
		[wMap1] = voteButton1,
		[wMap2] = voteButton2,
		[wMap3] = voteButton3,		
	}
	for i,v in pairs (bttImgTable) do
		if i:FindFirstChild("ImgID") then
			if i.ImgID.Value == (0) then
				v.Image = placeHolderImage
				print((i.Name) .. " ImgID is 0, please set a image for it!")
			else
				v.Image = i.ImgID.Value
			end
		else
			v.Image = placeHolderImage
			print((i.Name) .. " is missing child IntValue 'ImgID' please add one!")
		end
	end
end

-- update display choice titles
local function UpdateDisplayTitles()
	local bttTitleTable = {
		[wMap1] = choice1Title,
		[wMap2] = choice2Title,
		[wMap3] = choice3Title,
	}
	for i,v in pairs (bttTitleTable) do
		if i:FindFirstChild("RealName") then
			if i.RealName.Value == '' then
				v.Text = (placeholderText .. i.Name)
				print ("please put a RealName for" .. (i.Name))
			else 
				v.Text = i.RealName.Value
			end
		else
			v.Text = (placeholderText .. i.Name)
			print((i.Name) .. " is missing child StringValue 'RealName' please add one")
		end
	end
end

I will be combining both functions together as having them separate seems inefficient.

Before I do that I was wondering if there was a better way to do each function using less lines of code, or if there is an edge case that I might be forgetting about that might make this script error out.

1 Like