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

How to make keypress change text on a gui?

How to make keypress change text on a gui?

My script

local player = game.Players.LocalPlayer
local mouse  = player:GetMouse()
local gui = game.StarterGui.ScreenGui.Frame.active


mouse.KeyDown:connect(function(Key)
	if Key == "T" and gui.Parent.Visible == true and gui.Text == "Inactive (T)" then
		Key:Lower()
		gui.Text = "Active (T)" 

		
	elseif Key == "T" and gui.Parent.Visible == true and gui.Text == "Active (T)" then
		Key:Lower()
		gui.Text = "Inactive (T)" 

	
		
	end
	
end)

I have a gui called active and when I press "T" I want it to change text. How would I do this?

3 answers

✓ Accepted answer

You should be using UIS, since keyDown is deprecated, a correct example is shown down below :

local UIS = game:GetService("UserInputService")
local p = game.Players.LocalPlayer
local gui = p.PlayerGui.ScreenGui.Frame.active

UIS.InputBegan:Connect(function(Input, gameProcessedEvent) -- creates a Input function, and checks if the player isn't typing using gameProcessedEvent

local KeyCode = Input.KeyCode
if not gameProcessedEvent then

if KeyCode == Enum.KeyCode.T and gui.Parent.Visible == true and gui.Text == "Inactive (T)" then
gui.Text = "Active (T)"

elseif KeyCode == Enum.KeyCode.T and gui.Parent.Visible == true and gui.Text == "Active (T)" then
gui.Text = "Inactive (T)"
end
end
end)


If this doesn't work, please let me know, or any questions, please feel free to coment

--oSy

7 comments

0

Says some error at line 9 that there is a "(" missing?

0

when it does the UIS.InputBegan:Connect(function(Input, gameProcessedEvent) function

0

Sorry about that, i updated the script

0

The InputBegan is similar to KeyDown. InputEnded is again similar to KeyUp, which is when you release the key

0

What about mine?

0

Thanks!

0

Np, don't forget to accept the answer

That's deprecated, I think. Besides, you can't use LocalPlayer in a Script ;/ You can't also refer to StarterGui, you want to refer to PlayerGui: A folder generated when you click play, just like StarterGui, but that will be cloned into the Player. Put a regular script in the StarterGui folder, and paste the following:

--[[
local player = game.Players.LocalPlayer
local mouse  = player:GetMouse()
local gui = game.StarterGui.ScreenGui.Frame.active


mouse.KeyDown:connect(function(Key)
    if Key == "T" and gui.Parent.Visible == true and gui.Text == "Inactive (T)" then
        Key:Lower()
        gui.Text = "Active (T)" 


    elseif Key == "T" and gui.Parent.Visible == true and gui.Text == "Active (T)" then
        Key:Lower()
        gui.Text = "Inactive (T)" 



    end

end)
]]--

-- The correct code.
local player = script.Parent.Parent--Take advantage of the Script's place.
local mouse = player:GetMouse()
local gui = script.Parent.ScreenGui.Frame.Visible --Don't use Active: Use Visible.


function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.T then
		if gui == true then
			if gui.Text == "Inactive (T)" then
				gui.Text = "Active (T)"

			elseif gui.Text == "Active (T)" then
				gui.Text = "Inactive (T)"
			end
		end
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Also, make sure you wrote all the paths correctly.

If this works, please mark as the answer!

~~With love, by Pedro

1 comment

0

Active is a text button in my script.

game.Players.PlayerAdded:Connect(function(Player)
	local Va = Instance.new("StringValue") -- Adding a value
	Va.Value = Player.Name -- Making the value's value the players name for the GUI.
	local Val = Va.Value -- Just to make it quicker to type.
end)

local gui = game.Players.Val.PlayerGui.ScreenGui.Frame.active -- Getting the players GUI.

function onKeyPress(inputObject, gameProcessedEvent) -- If any key is pressed by that player
	if inputObject.KeyCode == Enum.KeyCode.T then -- If that key was the "T" key then
		if gui.Parent.Visible == true then
			gui.Text = "Active (T)"
		else
			gui.Text = "Inactive (T)"
		end
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress) -- Connecting the function.

2 comments

0

This code makes it so that it only changes on the players screen when they press "T".

0

Also, Make sure the script is a normal script, Not a local script.

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