Hello everyone,
I’m currently working on a Roblox project and I’m having trouble with a script that’s intended to use BodyGyro
to rotate an object when the spacebar key is pressed. However, it’s not functioning as expected, and I could use some assistance in identifying and resolving the issue.
Here’s a brief description of the problem:
- I’ve created a script that attaches a
BodyGyro
to an anchored object. - The script is designed to rotate the object by 45 degrees around the Y-axis when the spacebar key is pressed and stop when the spacebar key is released.
- Unfortunately, the object isn’t rotating as intended, and I can’t figure out what’s causing the issue.
Here’s the script I’m currently using:
local object = script.Parent
local gyro = Instance.new("BodyGyro")
gyro.D = 25
gyro.MaxTorque = Vector3.new(400000, 400000, 400000)
gyro.Parent = object
local isRotating = false
local function RotateObject()
isRotating = not isRotating
gyro.CFrame = CFrame.Angles(0, math.rad(45), 0)
end
local function StopRotation()
isRotating = false
gyro.CFrame = object.CFrame
end
local userInputService = game:GetService("UserInputService")
local inputIsProcessing = false
userInputService.InputBegan:Connect(function(input, isProcessed)
if not isProcessed and input.KeyCode == Enum.KeyCode.Space then
inputIsProcessing = true
RotateObject()
end
end)
userInputService.InputEnded:Connect(function(input, isProcessed)
if input.KeyCode == Enum.KeyCode.Space and inputIsProcessing then
inputIsProcessing = false
StopRotation()
end
end)