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

How do i make this KeyDown script do function after first function?

Asked by 6 years ago

I was trying to make a script that changed text of a GUI to something else after pressing "Z" button, but i could only make it do first function. How can i make it do second function after first function?


local mouse = game.Players.LocalPlayer:GetMouse() mouse.KeyDown:connect(function(key) key = key:lower() if key == "z" then One() end end) function One() script.Parent.Text = "..." end function Two() script.Parent.Text = "Test1" end function Three() script.Parent.Text = "Test2" end

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I think it would be easier if you just added a wait event between each function, or you could use a table as IdiomicLanguage stated. It's also best to use UserInputService, as KeyDown is deprecated.

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.Z then
        One()
        wait(3)
        Two()
        wait(3)
        Three()
    end
end

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

More information here. If this helped, please accept answer! Thanks!

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This is a little bit more powerful than you are asking for, but perhaps it will be a learning experience for you:



local curFunc = 0 local funcOptions = { function () script.Parent.Text = "..." end; function () script.Parent.Text = "Test1" end; function () script.Parent.Text = "Test2" end; } local function onMouseKeyDown(key) if key:lower() == "z" then curFunc = (curFunc % #funcOptions) + 1 funcOptions[curFunc]() end end) local mouse = game.Players.LocalPlayer:GetMouse() mouse.KeyDown:Connect(onMouseKeyDown)
0
Thank you for the help! Unity_456 47 — 6y
0
UserInput this way is deprecated, you should convert to UserInputService. Azarth 3141 — 6y
0
The script doesnt seem to work in game (but it works in studio). Also the problem is that i'm a beginner in scripting. Unity_456 47 — 6y
0
use Enums @Alexeeii greatneil80 2647 — 6y
View all comments (2 more)
0
I tried using them, they work in studio but not in game. Unity_456 47 — 6y
0
I found a way to fix it, just make the script local. Unity_456 47 — 6y

Answer this question