How would I make this rotate at a certain angle and stop, instead of just continuously spin?

I made a script so that if the player moves, a part roates. I want to make it so that when the player is moving, the part only has a certain angle it can rotate to, but instead it just rotates continuously.

My gun that I need to fix: https://gyazo.com/aefbe2aa391d0a6fb80a404e47678299

Script:
local Player = game:GetService’Players’.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local aks = Character:WaitForChild(“aks”)
local main = Character.aks.Main

repeat wait() until Player.Character

local RunService = game:GetService(“RunService”)

local function updateBobbleEffect()
local now = tick()
if Character.Humanoid.MoveDirection.Magnitude > 0 then
Player.Character.Torso.Motor6D.C0 = Player.Character.Torso.Motor6D.C0 * CFrame.Angles(0, 0, math.rad(0.5))
print((“lol”))
else
print(“no”)
end
end

RunService.RenderStepped:Connect(updateBobbleEffect)

You could store the original value of the motor’s C0, then have a variable store the current rotation, and then tweak that accordingly.

local Player = game:GetService’Players’.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local aks = Character:WaitForChild(“aks”)
local main = Character.aks.Main
local old_C0 = Player.Character.Torso.Motor6D.C0
local current_rotation = 0
repeat wait() until Player.Character
local RunService = game:GetService(“RunService”)
local function updateBobbleEffect()
local now = tick()
if Character.Humanoid.MoveDirection.Magnitude > 0 and current_rotation < 45 then
Player.Character.Torso.Motor6D.C0 = old_C0 * CFrame.Angles(0, 0, math.rad(current_rotation)))
current_rotation = current_rotation + .5
print((“lol”))
else
print(“no”)
end
end
RunService.RenderStepped:Connect(updateBobbleEffect)
4 Likes

Thank you so much! Your solution works, and I’ve learned from this!

1 Like

Quick question, could you help me real quick? When I try to modify it so that once I move and the gun rotates then if I stop then it will rotate back to its original position, but when I try to do that, this happens:https://gyazo.com/1b7a97aeccc66d89d9338b7b804bd1ed

Script: local Player = game:GetService’Players’.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local aks = Character:WaitForChild(“aks”)
local main = Character.aks.Main
local old_C0 = Player.Character.Torso.Motor6D.C0
local current_rotation = 0
repeat wait() until Player.Character
local RunService = game:GetService(“RunService”)
local function updateBobbleEffect()
local now = tick()
if Character.Humanoid.MoveDirection.Magnitude > 0 and current_rotation < 5 then
Player.Character.Torso.Motor6D.C0 = old_C0 * CFrame.Angles(0, 0, math.rad(current_rotation))
current_rotation = current_rotation + .5
print(“lol”)
else
print(“no”)
if Character.Humanoid.MoveDirection.Magnitude <= 0 and current_rotation < 5 then
Player.Character.Torso.Motor6D.C0 = old_C0 * CFrame.Angles(0, 0, math.rad(current_rotation))
current_rotation = current_rotation - .5

end
end
end
RunService.RenderStepped:Connect(updateBobbleEffect)

In your condition where you determine if the player stops moving, you could reset current_rotation to 0 and then set the motor’s C0 to the stored value, old_C0.

I’m not exactly sure how you want this to work

I’m trying to make the weapon slightly move whenever the player moves. It’s kinda like if you had something strapped on your back, if you move then the object might move a little but if you stay still then the object on your back doesn’t move at all.

So assuming if you just want the weapon to sway from say for example -20 degrees to positive 20 degrees when the player is moving, you don’t even need your other conditional statement to determine when the player isn’t moving, and you could tweak your first if statement a bit, but we might have to add another variable called current_direction or something so we know which direction we’re rotating the weapon.

So an example of this logic might be…

local current_direction = 1

if Character.Humanoid.MoveDirection.Magnitude > 0 then
current_rotation = current_rotation + (current_direction == 1 and .5) or - .5
if math.abs(current_rotation) >= 40 then
current_direction = (current_direction == 1 and 2) or 1
end
end

Doesn’t work for me for some reason, not sure if I applied it wrong or not.

local Player = game:GetService’Players’.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local aks = Character:WaitForChild(“aks”)

local main = Character.aks.Main

local old_C0 = Player.Character.Torso.Motor6D.C0

local current_rotation = 0

repeat wait() until Player.Character

local RunService = game:GetService(“RunService”)

local function updateBobbleEffect()

local now = tick()

local current_direction = 1

if Character.Humanoid.MoveDirection.Magnitude > 0 then

current_rotation = current_rotation + (current_direction == 1 and .5) or - .5

if math.abs(current_rotation) >= 10 then

current_direction = (current_direction == 1 and 2) or 1

Player.Character.Torso.Motor6D.C0 = old_C0 * CFrame.Angles(0, 0, math.rad(current_direction))

end

end

end

RunService.RenderStepped:Connect(updateBobbleEffect)

current_direction should be located outside of the updateBobbleEffect function.

On line 15, it says that I’m trying to perform arithmetic on a bool value.

1 local Player = game:GetService’Players’.LocalPlayer
2 local Character = Player.Character or Player.CharacterAdded:Wait()
3 local aks = Character:WaitForChild(“aks”)
4 local main = Character.aks.Main
5 local old_C0 = Player.Character.Torso.Motor6D.C0
6 local current_rotation = 0
7 repeat wait() until Player.Character
8 local RunService = game:GetService(“RunService”)
9 local current_direction = 1
10
11 local function updateBobbleEffect()
12 local now = tick()
13
14 if Character.Humanoid.MoveDirection.Magnitude > 0 then
15 current_rotation = current_rotation + (current_direction == 1 and .5) or - .5
16 if math.abs(current_rotation) >= 10 then
17 current_direction = (current_direction == 1 and 2) or 1
18
19 Player.Character.Torso.Motor6D.C0 = old_C0 * CFrame.Angles(0, 0, math.rad(current_direction))
20
21 end
22 end
23 end
24 RunService.RenderStepped:Connect(updateBobbleEffect)

Hm, try this instead:

 current_rotation = current_rotation + ((current_direction == 1 and .5) or - .5)

The error is gone now, but the problem is that the gun shifts, like I wanted it to, but when I stop, it doesn’t go back to its original position.

Script:
local Player = game:GetService’Players’.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local aks = Character:WaitForChild(“aks”)

local main = Character.aks.Main

local old_C0 = Player.Character.Torso.Motor6D.C0

local current_rotation = 0

repeat wait() until Player.Character

local RunService = game:GetService(“RunService”)

local function updateBobbleEffect()

local now = tick()

local current_direction = 1

if Character.Humanoid.MoveDirection.Magnitude > 0 then

current_rotation = current_rotation + ((current_direction == 1 and .5) or - .5)

if math.abs(current_rotation) >= 10 then

current_direction = (current_direction == 1 and 2) or 1

Player.Character.Torso.Motor6D.C0 = old_C0 * CFrame.Angles(0, 0, math.rad(current_direction))

end

end

end

RunService.RenderStepped:Connect(updateBobbleEffect)

Ah I see. Try this:

local Player = game:GetService’Players’.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local aks = Character:WaitForChild(“aks”)
local main = Character.aks.Main
local old_C0 = Player.Character.Torso.Motor6D.C0
local current_rotation = 0
repeat wait() until Player.Character
local RunService = game:GetService(“RunService”)
local function updateBobbleEffect()
local now = tick()
local current_direction = 1
if Character.Humanoid.MoveDirection.Magnitude > 0 then
current_rotation = current_rotation + ((current_direction == 1 and .5) or - .5)
Player.Character.Torso.Motor6D.C0 = old_C0 * CFrame.Angles(0, 0, math.rad(current_rotation))
if math.abs(current_rotation) >= 10 then
current_direction = (current_direction == 1 and 2) or 1
end
elseif Player.Character.Torso.Motor6D.C0 ~= old_C0 then
Player.Character.Torso.Motor6D.C0 = old_C0
end
end

The script that you suggested doesn’t work, the gun just stays still.

I made a mistake when writing the old_C0 variable, so it probably had a compilation error.

I’ve edited my post, try it now :slight_smile:

Yeah I caught that mistake too, I fixed it but the gun still stays still

1 Like

Oh I found it, when I went to rotate the C0 of the motor I used current_direction instead of current_rotation

I edited the post again, should work.

Hm, that’s certainly strange. It still doesn’t work for some reason.

I edited it again, I missed something else.

Still doesn’t work, I looked over the script and I can’t see any errors. The gun just doesn’t move anymore.