Using a rank list to change the text value of a rank text in my game, but I think there is a problem with the if and elseif statements?

In my script, there is a rank list that should, when the player joins, display their rank in a TextLabel. Everything worked until I added the if/elseif/else statements. I’m trying to find the problem with those. Can anyone help me out?

--ranklist
local owner = {"TheVeryBoredCat"}
local mod = {}
local vip = {}

game.Players.PlayerAdded:Connect(function(plr)
	local userId = plr.UserId
	local thumbType = Enum.ThumbnailType.HeadShot
	local thumbSize = Enum.ThumbnailSize.Size420x420
	local content = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
	local ui = plr.Backpack.BrickCard.card.SurfaceGui
	local ls = plr:WaitForChild("leaderstats")
	ui.pic.Image = content
	ui.user.Text = plr.Name
	ui.age.Text = "Days on Roblox: ".. plr.AccountAge
	ui.cash.Text = "BC$".. plr.leaderstats["BC$"].Value
	if plr.Name == owner then ui.rank.Text = "Owner"
	elseif plr.Name == mod then ui.rank.Text = "Mod"
	elseif plr.Name == vip then ui.rank.Text = "VIP"
		else ui.rank.Text = "Player"
	end
end)

you for got the "then" statements

Use table.find() This will help you achieve your goal.

Code example:

if table.find(mod, plr.Name) then
    ui.rank.Text = "Mod"
end

Also instead of this: elseif plr.Name == vip then ui.rank.Text = "VIP" else ui.rank.Text = "Player" end
And another elseif statement:

elseif plr.Name == vip then ui.rank.Text = "VIP"
elseif not table.find(vip, plr.Name) then ui.rank.Text = "Player"
end

The OP never forgot any ‘then’ statements, please be more observant next time.

The problem was that you were checking if the player’s name was owner, mod, or vip. But you can’t do that because they’re tables which hold values, like the player’s username for instance. A solution to this problem would be looping through the table and checking if the name in the table is equal to the player’s name. I’ve edited your script a bit, to loop through the table and check if the table value matches the player’s name. Also, you won’t have to worry about anyone (who is inside the tables) changing their usernames, because it will automatically check the current username. Another problem I have discovered was that you forgot to add the then keyword after the if statement. All conditions require the keywords if and then and end. For instance, a condition would like like this:

Example #1
local canPrint = true -- variable is true, so can print

if canPrint then
   print("Hello world")
end

-- If nothing prints, it would mean that the canPrint variable is false

You can use else or elseif statements in a code, but elseif would still require then and end. The end keyword will be at the end of the condition, you don’t add the end keyword at the end of every elseif statement.

Example of wrong usage
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	if plr.Name == "JohnDoe" then
		print("JohnDoe joined!")
	elseif plr.Name == "Player1" then
		print("Player1 joined")
	end -- this is irrelevant
	elseif plr.Name == "JaneDoe" then
       print("JaneDoe joined")	
	end -- this will work here, because it is at the end
end)
Example of correct usage
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	if plr.Name == "Player12345" then
		print("A player joined: Player12345")
	elseif plr.Name == "Hacker321" then
		print("A potentially dangerous person joined the game!")
	elseif plr.Name == "Moderator_Joe" then
		print("A moderator has joined the game")
	end
end)

To check for items in a table you can use table.find() or just loop through the table.

New Script
--ranklist
local owner = {"TheVeryBoredCat"}
local mod = {}
local vip = {}

local Players = game:GetService("Players")

local function getCurrentUsername(name)
	local UserId = Players:GetUserIdFromNameAsync(name)
	local currentUsername = Players:GetNameFromUserIdAsync(UserId)
	return currentUsername
end

local function playerNameInTable(Player, Table)
	for i,v in pairs(Table) do
		if getCurrentUsername(v) == Player.Name then
			return true
		end
	end
end

Players.PlayerAdded:Connect(function(plr)
	local userId = plr.UserId
	local thumbType = Enum.ThumbnailType.HeadShot
	local thumbSize = Enum.ThumbnailSize.Size420x420
	local content = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
	local ui = plr.Backpack.BrickCard.card.SurfaceGui
	local ls = plr:WaitForChild("leaderstats")
	ui.pic.Image = content
	ui.user.Text = plr.Name
	ui.age.Text = "Days on Roblox: ".. plr.AccountAge
	ui.cash.Text = "BC$".. plr.leaderstats["BC$"].Value
	if playerNameInTable(plr, owner) then
		ui.rank.Text = "Owner"
	elseif playerNameInTable(plr, mod) then
		ui.rank.Text = "Mod"
	elseif playerNameInTable(plr, vip) then
		ui.rank.Text = "VIP"
	else
		ui.rank.Text = "Player"
	end
end)

You can look more into tables and how to use them: here

1 Like

thank you so much man! this will help me a lot in my game!