My code is wrong, it didn't enter to the "if statment"

Hello! could someone help me with my code, please?

what is wrong? :frowning:

I am practicing with the page code

Its because gameprocessed didn’t return true so it went to the else statement and printed that.

1 Like

How could i fix that? :open_mouth:

gameProcessed means there was user input while you were focused inside of a text box or clicked on a gui element

1 Like

You’ve got to make gameprocessedevent return true somehow.

Oh something like this? :smiley:

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.A then
		local keyPressed = input.KeyCode
		gameProcessed = true
		print("A key is being pushed down! Key:",input.KeyCode)
		if gameProcessed then
			print("\tThe game engine internally observed this input!")
		else
			print("\tThe game engine did not internally observe this input!")
		end
	end
end)

If you don’t need to check for the gameProcessed event then don’t include it in the script. It is simply there to tell you if the user input was on a gui element.

1 Like

just put a “not” so it will “enter your if statement”

1 Like

No just keep the same script as before but do what hell_ish said.

1 Like

Another question… what is bad in my code? How could i use mouse buttons?

elseif input.KeyCode == Enum.UserInputType.MouseButton2 then
		local keyPressed = input.KeyCode
		-- Play animation track
		Animator:LoadAnimation(fastPunch):Play()
		print("Q key is being pushed down! Key:",input.KeyCode)
		if not gameProcessed then
			print("\tThe game engine internally observed this input!")
		else
			print("\tThe game engine did not internally observe this input!")
		end
if input.UserInputType == Enum.UserInputType.MouseButton1 then -- left mouse button
      -- code
end
if input.UserInputType == Enum.UserInputType.MouseButton2 then -- middle mouse button
      -- code
end
if input.UserInputType == Enum.UserInputType.MouseButton3 then -- right mouse button
      -- code
end
1 Like

What are you trying to achieve exactly?

1 Like

I am learning to program with roblox and I have this as part of my ways of learning :smiley:

Okay, gameProcessed exists to you can check if the player was typing inside a TextBox, for example you try to check for the key Q being held, the function activates but the player is actually chatting, if Q is supposed to attack for example, the player will attack while he meant to type in the chat, if you use if not gameProcessed then when the player uses the Q key, it will only attack if the player wasn’t typing inside a TextBox

Got it?

The second parameter to InputBegan returns a boolean indicating whether a player interacted with a GUI (graphical user interface), typing in a TextBox such as the in-game chat, or the settings menu.

If the player was doing such any of the above previously mentioned, the return value would be true - however, if none of those were done, the return value would be false.

To break it down into code,

UserInputService.InputBegan:Connect(function(KeyInfo, GameProcess)
    if GameProcess then -- This includes GUI interactions, typing in a TextBox, or being in the settings menu.
        print("Was a game process!")
    else -- If the player didn't do any of the previously mentioned.
        print("Was not a game process!")
    end
end)

It will still return the inputted character, but the boolean’s there if you want to prevent specific code from executing from, say, a player typing in the chat and your custom inventory opening up because of it.

UserInputService.InputBegan:Connect(function(KeyInfo, GameProcess)
    if GameProcess then
        print("Was a game process!")
    else
        if KeyInfo.KeyCode == Enum.KeyCode.I then
            InventoryGui.Enabled = not InventoryGui.Enabled
        end
    end
end)

I hope this helped. :slight_smile: