Help with "If [blank] is less than [blank] then" statements

I was making a script that made it so that when the currency, (in this case, years), was 2016 or less, it would open a door. My script doesn’t work. Could the reason be that the value of the currency has a comma in it?

Script:

local Player = game.Players.LocalPlayer
if Player.leaderstats.Year_Joined.Value <= "2016" then
	game.Workspace.TwentySixteenDoor.Transparency = 1
	game.Workspace.TwentySixteenDoor.CanCollide = false
end
Script Details

Type: LocalScript
Parent: StarterPlayerScripts

Screenshot of currency amount:
image

If the problem is the commas, how can I get rid of them?
Thanks for reading!

Your data types are messed up. You’re trying to compare a number to a string.

Try: if Player.leaderstats.Year_Joined.Value <= 2016 then

2 Likes

It’s not the commas, but to compare just the number part of a string:

if str:match("%d+") == 2016 then 

end

Calling tonumber on the string “2,016” would return nil since it is not a valid number, though in this case it is just shown formatted on the leaderboard - the value is still 2016.

1 Like

When comparing strings with those operators, it will compare them alphabetically rather than numerically. Consider converting them to numbers first:

if tonumber(Player.leaderstats.Year_Joined.Value) <= 2016 then
4 Likes

Still doesn’t work…

30 characters

Use StringValues, are you setting this to IntValues because IntValues in leaderboard automatically formats the number depnding on the locale (2,016 for English, 2.016 for German and 2 016 for French)?

2 Likes

Okay, I’ll try that! It should work!

When you say it still doesn’t work, are you getting any sort of error? @Asinphi had a good suggestion by adding tonumber() around the Year_Joined object’s Value

Okay, now that I did StringValues, the currencies look like this:
image

I tried it, and still nothing…

I’ve tested it in an online Lua interpreter and I found out that commas do indeed confuse Lua’s tonumber(). However, there are clever things you can do with strings to remove the comma:

local str = Player.leaderstats.Year_Joined.Value
local parts = str:split(',')
if tonumber(parts[1] .. parts[3]) <= 2016 then

This is just one of them.

So I wrap the code in that, like this?

local Player = game.Players.LocalPlayer
if str:gsub("%d+") == 2016 then 
game.Workspace.TwentySixteenDoor.Transparency = 1
game.Workspace.TwentySixteenDoor.CanCollide = false
end	


Maye round down the value beforehand?

[Insert StringValue here].Value = math.floor(value)
1 Like

No - I mixed up match and gsub, fixed now.

@Asinphi

That won’t affect anything here, it’s not the string that’s being compared - it is just shown formatted depending on the locale as Blockzez said but the value is still a number for IntValue objects.

tonumber(v.Value) == v.Value -- for any value 

Or you know just

if str:match("%d+") <= 2016 then end

Even then, since it is just an integer value and won’t actually contain any commas, you could do

if v.Value <= 2016 then end
1 Like

Would I round down in the script that instances the currency?

Yes, unless you need the decimal place.

1 Like

Math.floor worked! Now, to get the script to work…

Could the problem be that the script is inside of StarterPlayerScripts? When I ran the code like this, it didn’t work either, and everything looked valid:

local Player = game.Players.LocalPlayer
if Player.leaderstats.Year_Joined.Value == 2016 then
game.Workspace.TwentySixteenDoor.Transparency = 1
game.Workspace.TwentySixteenDoor.CanCollide = false
end

image
@Blockzez? @XxELECTROFUSIONxX?

Errors would be helpful.
Try calling WaitForChild on the leaderstats since it won’t exist implicitly.

1 Like

FYI, you’re doing == 2016 and not >=, plus are you detecting when the value is changed?
Edit: This issue is not StarterPlayerScripts and it looks like you arent actually detecting .Changed

1 Like