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?
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
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
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.