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

How to call on the "UserInputService" correctly?

Asked by 9 years ago

In my script I want to have the Left Alt key change the lettering inside of a GUI. The script is a LocalScript located inside StarterPack. For some strange reason when I go to test it, the lettering doesn't change but instead says "Attempt to change a nil value" As you can see I'll be adding more code below that will change the leg used. I haven't added that code yet due to the fact that this simple thing isn't working, it's verryyy frustrating. Thanks for your help! EDIT: I have tested it with a different button than LeftAlt and still yields the same result.

CF = game.StarterGui.ChooseFoot.Frame.TextLabel

game:GetService("UserInputService").InputBegan:connect(Pressed)

function Pressed(key)
        if key == Enum.KeyCode.LeftAlt then
            if CF.Text == "R" then
                CF.Text = "L"
            elseif CF.Text == "L" then
                CF.Text = "R"
            end
        end
end

2 answers

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

You have your connection statement above the definition of the function - when the script reads the connection statement 'Pressed' is a nil value. Define the function BEFORE the connection statement.

local CF = game.StarterGui.ChooseFoot.Frame.TextLabel

function Pressed(key)
    if key == Enum.KeyCode.LeftAlt then
        if CF.Text == "R" then
            CF.Text = "L"
        elseif CF.Text == "L" then
            CF.Text = "R"
        end
    end
end

game:GetService("UserInputService").InputBegan:connect(Pressed)
0
It didn't yield "attempt to call an nil value" but it still didn't change the Text in the GUI. The GUI path goes: game.StarterGui.ChooseFoot.Frame.TextLabel.Text Thanks for the help too, you've answered a similar question of mine regarding the use of left alt :) Ghost4Man 25 — 9y
Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

Your second problem is a very common one -- StarterGui is NOT what a player sees.

StarterGui is simply a container that stores GUIs

GUIs in StarterGui will be cloned into a Player's PlayerGui when they join and when they respawn -- this is why they are visible.

You only see what's in your PlayerGui.

GUIs in StarterGui are only visible in studio for editing purposes.


Therefore, when editing GUIs make your changes in a Player's PlayerGui. PlayerGui is, however, local, meaning if you want every player's GUIs to be edited you must loop through the players with a for loop and edit each GUI individually.

Answer this question