How would I make the script print "Hello World!" as long as the space bar is held down? Should I use ContextActionService or UserInputService? Does that even affect it? Here's what I have so far:
function onKeyPress(actionName, userInputState, inputObject) while userInputState == Enum.UserInputState.Begin do wait() print("Hello World!") end end game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.Space)
Instead of using the ContextActionService use UserInputService and a repeat loop.
It will run once there is input, if it's a Regular script, it runs when ANY player does any input. If it's a local script then it only runs once a certain
local userinputservice = game:GetService("UserInputService") userinputservice.InputBegan:connect(function(key, processed) if key.KeyCode == Enum.KeyCode.Space and not processed then --Processed is like, chatting/typing into a textbox. print("Pressed space") end end)
This runs once you stop inputting.
local userinputservice = game:GetService("UserInputService") userinputservice.InputEnded:connect(function(key, processed) if key.KeyCode == Enum.KeyCode.Space and not processed then --Processed is like, chatting/typing into a textbox. print("Stop pressing space") end end)
This loops until something is true
local a = 0 repeat a = a+1 wait() --Add some type of yield to prevent game from crashing until a > 5
local userinputservice = game:GetService("UserInputService") local looped = false userinputservice.InputBegan:connect(function(key,processed) if key.KeyCode == Enum.KeyCode.Space and not processed then looped = true repeat --Code goes here wait() until not looped end end) userinputservice.InputEnded:connect(function(key, processed) if key.KeyCode == Enum.KeyCode.Space and not processed then looped = false end end)
Hope it helps!