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

While a keyboard button is being pressed?

I'm having trouble finding code that can do this, but is there any way i can make this script so that WHILE this button is held down, it does the following;?

local player = game.Players.LocalPlayer 
mouse = game:GetService("Players").LocalPlayer:GetMouse()
mouse.Icon = "http://www.roblox.com/asset/?id=398521199"

mouse.KeyDown:connect(function(key)
if key == "e" then
game.Workspace.Part.BodyAngularVelocity.AngularVelocity = Vector3.new(0,1,0)
end
end)



mouse.Button1Down:connect(function()
	player = game.Players.LocalPlayer
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)  
end)

Would be extremely grateful if I could be told what to do, or if you could even do it for me, but thanks all in all!

1 answer

✓ Accepted answer

First of all, I'm going to try to try to push you away from using BodyMover objects. They are great, simple and make problems easy, but it's also nice to know how to do it without these objects. However I will post two solutions, one for each of these methods.

KeyDown is a deprecated method, meaning it only exists for compatibility purposes. You should be using the service UserInputService to get, well, user input.

All we need to know is when the key is pressed, and when the key is released. One way to do this would be to use an InputBegan and InputEnded event, but it turns out this is unnecessary. We can instead just create a loop when InputBegan is called that continues until the key is no longer down. We'll use the IsKeyDown function to determine when it is released.

Because we want it to rotate continuously, we'll need to update it every time the scene is updated (rendered). We can use the service RunService to wait until it should update again.

###CFrame Method:

local UserInputService = game:GetService("UserInputService")
local part = game.Workspace.Part

UserInputService.InputBegan:connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.E and not gameProcessed then
		repeat
			local elapsed, _ = game["Run Service"].RenderStepped:wait()
			part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(90)*elapsed, 0)
		until not UserInputService:IsKeyDown(Enum.KeyCode.E)
	end
end)

Note: Speed can be changed by adjusting the radian amount on line 8 (radians per second)

###BodyMover Method:

local UserInputService = game:GetService("UserInputService")
local bodyAngularVelocity  = game.Workspace.Part.BodyAngularVelocity 

UserInputService.InputBegan:connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.E and not gameProcessed then
		bodyAngularVelocity.AngularVelocity = Vector3.new(0, 1, 0)
	end
end)

UserInputService.InputEnded:connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.E and not gameProcessed then
		bodyAngularVelocity.AngularVelocity = Vector3.new(0, 0, 0)
	end
end)

3 comments

0

I'll try it out, thanks.

0

THANKS SO MUCH MAN! :D

0

No problem :)

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