Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Level up system using >= for text?

Asked by
AltNature 169
6 years ago
Edited 6 years ago

So, I'm trying to make a level up system for a game I'm making but I get an error when I try to compare a string to a number. I can't figure out how I would do it since it it can only be text.

--this is an example
local XP = script.Parent -- is a textlabel that displays how much XP they have
local LVL = script.Parent.Parent.TextLabel2 -- TextLabel that displays the level

if XP.text >= 2000 then --[[ This is what I need help with, I can't quite figure out how I would compare it to string.]]
        LVL.text = 2
  --then it would show a pop up that says you leveled up
    wait(3)
    --you leveled up pop up disappears.
end

I hope you can help ! if you can, THANKS!

1
I need to know too OBenjOne 190 — 6y
0
Usually when i use xp or levels i place the number inside a numbervalue to make life easier 20002000sa 83 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago

You can do this with the tonumber function. It attempts to convert its argument to a number, will throw error if it cannot (e.g. you passed letter arguments or userdata arguments). Because of that, you may want to wrap tonumber in a pcall (protected call).

local success, message = pcall(function()
    if tonumber(XP.Text) >= 2000 then
        -- code 
    end
end)
1
Thanks, been trying to figure this out for a while now, so I'll definetly accept it. AltNature 169 — 6y
Ad

Answer this question