Why isn't my script working?

Hello, so I have a script that is meant to press a button to do a function. But for some reason, it isn’t working. and as soon as I playtest the game, in the output, it prints “fail”, can anyone tell me why this isn’t working?

`

local userInput = game:GetService("UserInputService")

userInput.InputBegan:Connect(function(input,gameProcessed)
	if gameProcessed then -- Prevents this from running when typing in chat
		if input.KeyCode == Enum.KeyCode.E then
			print("success")
		else
			print("fail")
		end
	end
end)

`

Still waiting on a response :slightly_smiling_face: (31 char 310 char 69 char 420 char)

Well are you pressing E?.. [ filler ]

1 Like

Im pretty sure they would press E if they made this script to work for E

2 Likes

Yes, which does nothing currently. Also, as soon as I playtest the game, it just prints “fail”

Is this in a client script? LocalScript?

1 Like

This script is in the current state of a local script in the StarterGui.

Were you holding a button before entering your game?

1 Like

No, I was not holding a button before entering the game.

Hmmm, pretty confusing ngl. car cap

1 Like

Try doing this instead…

local userInput = game:GetService("UserInputService")

userInput.InputBegan:Connect(function(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.E and gameProcessed then
		print("success")
	else
		print("fail")
	end
end)
3 Likes

ive tried the script, it doesnt work when i press e, but when i removed game proccessed works

2 Likes

When I press any key, it now prints “fail”, which is a step-up from before. but also pressing ‘E’ makes it print “fail”. hmm

Oh, I believe game processed stops it from occuring when the player types “E” while in chat though

Never mind, it still prints fail no matter what

1 Like

Then try putting not in front of the gameProcessed

3 Likes

maybe its the else statment thats breaking the code? i may sound stupid

1 Like

this is a solution, it works :slight_smile:

3 Likes

thanks to everyone who replied and try to help. this solution worked, thanks!

2 Likes

GameProcessed is a Boolean parameter indicating if or not the game handled (processed) the input.

1 Like