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

How to make keypress change text on a gui?

Asked by 7 years ago
Edited 7 years ago

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

Log in to vote
0
Answered by
oSyM8V3N 429 Moderation Voter
7 years ago
Edited 7 years ago

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

0
Says some error at line 9 that there is a "(" missing? The_sandwic 14 — 7y
0
when it does the UIS.InputBegan:Connect(function(Input, gameProcessedEvent) function The_sandwic 14 — 7y
0
Sorry about that, i updated the script oSyM8V3N 429 — 7y
0
The InputBegan is similar to KeyDown. InputEnded is again similar to KeyUp, which is when you release the key oSyM8V3N 429 — 7y
View all comments (3 more)
0
What about mine? LennonLight 95 — 7y
0
Thanks! The_sandwic 14 — 7y
0
Np, don't forget to accept the answer oSyM8V3N 429 — 7y
Ad
Log in to vote
0
Answered by
OfcPedroo 396 Moderation Voter
7 years ago
Edited 7 years ago

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

0
Active is a text button in my script. The_sandwic 14 — 7y
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago
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.

0
This code makes it so that it only changes on the players screen when they press "T". LennonLight 95 — 7y
0
Also, Make sure the script is a normal script, Not a local script. LennonLight 95 — 7y

Answer this question