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

How to call on the "UserInputService" correctly?

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

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)

1 comment

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 :)

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.

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