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

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

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

✓ Accepted answer

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!

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)

6 comments

0

Thank you for the help!

0

UserInput this way is deprecated, you should convert to UserInputService.

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.

0

use Enums @Alexeeii

0

I tried using them, they work in studio but not in game.

0

I found a way to fix it, just make the script local.

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