Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does it do nothing when equals is pushed?

When equals is pushed the gui stays the same and does not change. Here is the code:

function onInputBegan(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local keyPressed = input.KeyCode
		script.Parent.Text = keyPressed
		
function onKeyPress(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.Equals then
		print("= was pressed")
		script.Parent.Parent.Junior = script.Parent.Text
		script.Parent.Text = " "
	end
	end
	end
	end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
game:GetService("UserInputService").InputBegan:connect(onInputBegan)

Here is the error I'm getting:

18:19:31.821 - Attempt to connect failed: Passed value is not a function
18:19:31.821 - Stack Begin
18:19:31.822 - Script 'Players.Player1.PlayerGui.ScreenGui.Frame.TextBox.LocalScript', Line 15
18:19:31.822 - Stack End

Is there anything else you want to know? Say a comment... I suck at questions sorry...

Thanks-arrowman888

1 answer

Properly. Tab. Your. Code.

After fixing the indentation, the problem is obvious:

function onInputBegan(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local keyPressed = input.KeyCode
        script.Parent.Text = keyPressed
        
        function onKeyPress(inputObject)
            if inputObject.KeyCode == Enum.KeyCode.Equals then
                print("= was pressed")
                script.Parent.Parent.Junior = script.Parent.Text
                script.Parent.Text = " "
            end
        end
    end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
game:GetService("UserInputService").InputBegan:connect(onInputBegan)

When you use connect, onKeyPress doesn't exist yet, therefore it's nil. It's not a function, which causes an error. There's a better way to do this anyway:

game:GetService("UserInputService").InputBegan:connect(function()
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local keyPressed = input.KeyCode
		script.Parent.Text = keyPressed
		if input.KeyCode == Enum.KeyCode.Equals then
			print("= was pressed")
			script.Parent.Parent.Junior = script.Parent.Text
			script.Parent.Text = " "
		end;
	end;
end);

Hope this helped. Read more about Input & Anonymous Functions.

This discussion is preserved as it was. New answers, comments, and votes are closed.