I have this small script to check if the W button is pressed.
local UIS = game:GetService("UserInputService")
while true do
if UIS:IsKeyDown(Enum.KeyCode.W) then print("W") else print("notW") end
wait()
end
When I test the script, it doesnt output any button presses. I have no idea what the issue could be, I tried to follow the code example on the documentation page, but it doesnt make any sense to me, I also asked a scripter and he also couldnt tell whats wrong with it.
I dont really need anything more than getting this check to work, so Id appreciate any help on this.
I tested this out, and it seems to work fine. Make sure your script is a local script, and somewhere that replicates to the client(starterplayer, startertools, startergui)
2 Likes
this is a REALLY REALLY inefficient and memory heavy way of checking. I recommend using context actions service instead
1 Like
This is a pretty bad way to detect if W is being held. You should always avoid while wait()
loops by using built in functions to listen for what you want. You can use UserInputService’s InputBegan
and InputEnded
functions to listen for the W key being pressed and released.
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.W then
print("pressed W")
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.W then
print("released W")
end
end)
2 Likes
Well, alright
I put this script in an empty, new place and I cant get any outputs of it either. Is there something I need to do to make it work before?
Where is your script located? This should be a LocalScript, which can only be run in StarterCharacterScripts, StarterPlayerScripts, and StarterGui.
1 Like
Im an idiot and theres no better way to describe it lol
Fixed it