Attempt to compare string >= number

Hello fellow developers!

I am creating a pay command however I keep getting this error:

Here is my code:

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
			
		local Command = string.split(Message, " ")
		local PayCommand = string.lower(Command[1])
		
		if PayCommand == "/pay" then
			
			local PaidPlayerName = Command[2]
			
			local Amount = Command[3]
			
			local PaidPlayer = game.Players:FindFirstChild(PaidPlayerName)
			
			if PaidPlayer ~= Player.Name then --If the paid player's name isn't the paying player's name, then
				
				if Player.leaderstats.Cash.Value >= Amount then --Checking if the player has enough money (This is the part where I begin to get the error)
					
					if PaidPlayer.leaderstats then --If the second word is actually a player and they have leaderstats, then

						PaidPlayer.leaderstats.Cash.Value = PaidPlayer.leaderstats.Cash.Value + Amount
						Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - Amount
					end
				end
			end
		end
	end)
end)

I have been searching for an answer for hours but could not find one.

This means that Player.leaderstats.Cash.Value is a string. Change the value to a NumberValue.

I have done that and it still doesn’t work, I keep getting the same error.

I would print out both Player.leaderstats.Cash.Value and Amount and their types using typeof().

print(Player.leaderstats.Cash.Value)
print(typeof(Player.leaderstats.Cash.Value))
print(Amount)
print(typeof(Amount)

It’s detecting Amount as a string instead of a number. As you can see when I type it out, it says 10.

Use tonumber on amount and it should fix the issue.

Small error here that is not relevant to the real issue, but you are comparing a player instance with a string; it will always return true.

Like this?

print(typeof(Player.leaderstats.Cash.Value))
			tonumber(Amount)
			wait(2)
			print(typeof(Amount))

Because if so then it’s not working.

You need to set amount to the actual number.

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
			
		local Command = string.split(Message, " ")
		local PayCommand = string.lower(Command[1])
		
		if PayCommand == "/pay" then
			
			local PaidPlayerName = Command[2]
			
			local Amount = tonumber(Command[3])
			
			local PaidPlayer = game.Players:FindFirstChild(PaidPlayerName)
			
			if PaidPlayer ~= Player.Name then --If the paid player's name isn't the paying player's name, then
				
				if Player.leaderstats.Cash.Value >= Amount then --Checking if the player has enough money (This is the part where I begin to get the error)
					
					if PaidPlayer.leaderstats then --If the second word is actually a player and they have leaderstats, then

						PaidPlayer.leaderstats.Cash.Value = PaidPlayer.leaderstats.Cash.Value + Amount
						Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - Amount
					end
				end
			end
		end
	end)
end)

A bunch of other random issues with the cleanliness of your code:

  • Don’t use game.Players, use game:GetService("Players"), since it doesn’t rely on the name of the service instance.
  • It might be worth making Player.leaderstats.Cash and PaidPlayer.leaderstats variables just to make the code a bit cleaner.
1 Like

It works! Thank you so much!

Lol, I just wanted to reference Player faster. It still works the same.