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
I'll try it out, thanks.
THANKS SO MUCH MAN! :D
No problem :)