Hey Guys!
I’m currently working on a project with a couple of buds and i’m working on the guns, now. I got this problem with the code. when you aim, we don’t use view model, we use c frame to change the arm position based of mouse position, and this happens once we enable the script. But the problem is once you disable the script, it looks like this.
https://gyazo.com/f3d97d107c0b6f04f600e97628c00c16
Here is the code:
wait(1)
local plr = game.Players.LocalPlayer;
repeat wait() until plr.Character
local char = plr.Character
local m = plr:GetMouse()
game["Run Service"].RenderStepped:connect(function()
local c = game.Workspace.CurrentCamera
char.Torso["Right Shoulder"].C0 = CFrame.new(1,0.5,0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y),1.55,0)
char.Torso["Left Shoulder"].C0 = CFrame.new(-1,0.5,0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y),-1.55,0)
char.Torso["Neck"].C0 = CFrame.new(0,1,0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y) + 1.55,3.15,0)
end)
Is there anyway to fix this, so the arms to back to normal?
Any help loved!
To solve this, I think you should save the original cframes of the character’s motor6ds before you change them and just reset them to that once you’re done.
I also noticed a couple other things with the script:
- You don’t need a
wait(1)
at the top of the script, the localplayer is always there.
- Instead of using
repeat wait() until
, you should use the CharacterAdded event.
- The way you’re getting the services is deprecated, you should use
:GetService
.
-
:connect()
is deprecated, you’re supposed to use :Connect()
.
- The script won’t work for r15 characters.
-
.p
is deprecated, you should use .Position
.
- Same as above with
.Unit
instead of .unit
.
- You should define camera once outside of the RenderStepped connection instead of every single frame.
- Please indent, it’ll make fixing the scripts easier for us and for yourself.
- Use proper variable names for the same reason as number 9.
- Save the math stuff in a variable instead of re-doing it every single time. This is especially important because the code runs like 60 times a second.
- The thing I said above also applies for the character’s body parts.
- The script will stop working once the character resets.
Here’s the script with the above things I mentioned fixed: (apart from the r15 thing)
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()
local rightShoulder = char.Torso:FindFirstChild("Right Shoulder")
local leftShoulder = char.Torso:FindFirstChild("Left Shoulder")
local neck = char.Torso:FindFirstChild("Neck", true)
-- reset the c0s to these once u stop the renderstepped
local originalRightShoulderC0 = rightShoulder.C0
local originalLeftShoulderC0 = leftShoulder.C0
local originalNeckC0 = neck.C0
RunService.RenderStepped:Connect(function()
local angleComponent = -math.asin((mouse.Origin.Position - mouse.Hit.Position).Unit.Y
rightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(angleComponent, 1.55, 0)
leftShoulder.C0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(angleComponent, -1.55, 0)
neck.C0 = CFrame.new(0, 1, 0) * CFrame.Angles(angleComponent + 1.55, math.pi, 0)
end)
plr.CharacterAdded:Connect(function(newChar)
rightShoulder = newChar.Torso:FindFirstChild("Right Shoulder")
leftShoulder = newChar.Torso:FindFirstChild("Left Shoulder")
neck = newChar.Torso:FindFirstChild("Neck", true)
end)
1 Like