Need help on overhead GUI

The problem
Hi there! I’m trying to make an overhead system, every time a player levels 5 levels up, it gets a new rank, here’s the code I use:

local ranksNames = {"Eggy", "Novice", "Artist", "Warrior", "Innovator", "Fighter", "Excellent", "Elite", "Adventurer", "Trusty", "Navy", "Master", "Epic", "Prominent", "Enthusiast", "Legend",  "Wealthy", "The Almighty Shrek"}
						
overheadText.Text = ranksNames[math.floor(level.Value + 0.5) * 5]

But every time I’m trying to run it, I get this error:
invalid argument #3 (string expected, got nil)

Thanks for reading and I hope you can help me out :slightly_smiling_face:

2 Likes

I added a tostring. this should work:

local ranksNames = {"Eggy", "Novice", "Artist", "Warrior", "Innovator", "Fighter", "Excellent", "Elite", "Adventurer", "Trusty", "Navy", "Master", "Epic", "Prominent", "Enthusiast", "Legend",  "Wealthy", "The Almighty Shrek"}
						
overheadText.Text = tostring(ranksNames[math.floor(level.Value + 0.5) * 5])
6 Likes

Was just about to reply, yes tostring fixes it ^

What is your level.Value? Like, what is it equal to, as it might be higher than you have indexes in the table.

It’s equal to the player’s level stats.

But what is the players level that you are testing with right now?

1 Like

The error actually explains what goes wrong kinda well.

You’re trying to set the Text property to nil, which naturally doesn’t make sense because nil can’t represent text. Only strings can (string expected, got nil).

If you’d tested your math, you’d have seen that it’s wack. For the levels 1 to 10, it gives these “rank numbers”:

1 -> 5
2 -> 10
3 -> 15
4 -> 20
5 -> 25
6 -> 30
7 -> 35
8 -> 40
9 -> 45
10 -> 50

Where the number before the arrow is the level and the number after is the rank. Right now you’re giving players 5 ranks per level instead of one rank for every 5th level. There’s no rank number 25, so when you try to set the Text property to ranks[25], you’re trying to set Text to nil. Hence the error.

Here’s the actual rounding function you want:

math.ceil(i/5)

Yeah that simple. You can test the two approaches like this:

for i = 1, 10 do
    print(i .. " -> " .. ( math.floor(i + 0.5) * 5 ))
end

for i = 1, 15 do
    print(i .. " -> " .. math.ceil(i/5))
end

It’s often fine to fudge things by “just getting it to work” (adding a tostring in this case), but it’s usually better to really understand what goes wrong so you can fix the underlying cause.

5 Likes

I’m guessing you fixed this? But it’s because you are * 5, I did this * 1 and it completely worked for me. bimage y

^ @ThanksRoBama is also correct as I didn’t realise about how you are doing the math of each level.