Is there any way to make this script detect when I press W and let go of W?
local userInputService = game:GetService("UserInputService")
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.W then
print("You pressed W")
end
end
end)
An extra thought, is there any way to get a part to move wherever I want it to (constantly) when I press W, and for it to stop when I let go of W?
Any help is appreciated!
1 Like
Oh this is easy, you should use UserInputService.InputEnded
for this, you should watch this UIS.InputEnded
2 Likes
For the second thing, use RunService
or you can use a While
to move the part, but i don’t like using while for this…
then do a code like this:
While wait() do
part.Position += Vector3.new(0,0,0.1)
end
and when it ends, make that it does nothing.also, this is only an example, this would be in a RemoteEvent
of course
1 Like
I figured out the InputEnded function, so it tells me when I press and release Q.
local userInputService = game:GetService("UserInputService")
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.Q then
print("You pressed Q")
end
end
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.Q then
print("You released Q")
end
end
end)
Where should I put the code to make the part move?
1 Like
Ok, for this, you should use RemoteEvents
, if you don’t know what it is, then watch this helpful video.
i used this to learn RemoteEvents
before, and it works perfectly.
It doesn’t seem to be working.
Here is the code from the first script, where I implement the function.
local userInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MovePart = ReplicatedStorage:WaitForChild("MovePart")
local MovePartFunction = ReplicatedStorage:WaitForChild("MovePartFunction")
local Part = game.Workspace.Part
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.Q then
print("You pressed Q")
MovePartFunction:InvokeServer(Part)
end
end
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.Q then
print("You released Q")
end
end
end)
And then here is the other script that connects to the last one.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MovePart = ReplicatedStorage:WaitForChild("MovePart")
local MovePartFunction = ReplicatedStorage:WaitForChild("MovePartFunction")
MovePartFunction.OnServerInvoke = function(player, Part)
Part.Position += Vector3.new(0,0,0.1)
end
I have a RemoteEvent and a RemoteFunction in ReplicatedStorage, and also the second script is in there, too. I’m not sure if it is supposed to be there, or if it is supposed to be a LocalScript.
1 Like