Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does it do nothing when equals is pushed?

Asked by 7 years ago

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

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

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.

Ad

Answer this question