Using UserInputService.InputBegan in an If loop isn't working how I want it to

The following code is a simplified version of an extract from a localscript I’m using in a grid placement system:

local LocalPlayer = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
if 2<5 then
UserInputService.InputBegan:Connect(function(input,gameproccessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Z then
			print("Z was pressed")
		end
	end
end)
end

The problem is that the code inside the if loop is executed even when the condition is not met (in my case 2 is not greater than 5; still, when I press Z on my keyboard, the message “Z was pressed” pops up in the output).
I’ve tried making different variants of the code, but the problem persists. I’ve read the article on If Statements on the Roblox Developer Hub, and nothing there explains why this should be occurring.
It would be very helpful to me if somebody explained to me why this is happening.

if 2<5 means if 2 is less than 5, and not greater than.

1 Like

< means less than. Replace it with >

1 Like

@uhi_o @P1X3L_K1NG thanks!
This was a really stupid typo on my part. I was so busy with the code inside the If loop that I didn’t pay any attention to the condition itself.
Thank you for pointing this out!

1 Like

No problem! Glad we could help :slight_smile:

1 Like