Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Weld/CFraming and movement not working?!?!?!?

Asked by 9 years ago
local ArmWeld = Instance.new("Weld")
    ArmWeld.Parent = Torso
    ArmWeld.Part0 = Head
    ArmWeld.Part1 = RArm
    ArmWeld.C1 = CFrame.new(0,0,0)

    local HeadWeld = Instance.new("Weld")
    HeadWeld.Parent = Torso
    HeadWeld.Part0 = Head
    HeadWeld.Part1 = LArm
    HeadWeld.C1 = CFrame.new(0,0,0)

    game:GetService("RunService").RenderStepped:connect(function() --Runs the function each time a frame is rendered.
    Mouse.TargetFilter = workspace --This is important when setting the C0 at the bottom as it ignores everything in the workspace to give clean movement of the arms on the Y axis.
    Player.Character.Torso.Neck.C1 = CFrame.new() --Sets the C1 to a blank CFrame so the head doesn't glitch.
    Player.Character.Torso.Neck.C0 = CFrame.new(0, 1.5, 0) * CFrame.Angles(math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y), 0, 0) --Moves the head up by 1.5 studs and then sets the angle of your character's neck looking up towards the mouse using inverse trigonometry (A grade maths) and unit vectors (so you only get the direction.)
    end)

This is what I have so far, the problem is that the head gets welded to the arm and doesn't move, I need the arm to stop moving and move with the head, the bottom portion of this script moves the head with the mouse perfectly fine, yet when I weld the arms to the head, the head no longer moves but the arms continue to move, doesn't matter whether I change the C0 and C1 properties.

I am eventually trying to get to the point where the entire character rotates with the mouse in 3rd person and the arms move up and down while gripping the gun.

Any help appreciated, leave a rep if this helped anyone.

1 answer

Log in to vote
1
Answered by 9 years ago

The problem with your head being stuck to your arm is the fact that the Right Shoulder and Left Shoulder parts are welded to the head already and are conflicting.

To fix this, you need to set Part1 of each shoulder weld to nil. This makes it so the head is free to move and the arms follow with it.

You may also need to change the C1 of the arms after this as they will be in your head until you offset them correctly.

Also, to prevent dying, you need to use two different events with the same function: The Move event and the Idle event in the mouse.

I have tested the below script and it works perfectly.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Char = Player.Character or Player.CharacterAdded:wait()

local Torso = Char.Torso
local Head = Char.Head
local RArm, LArm = Char["Right Arm"], Char["Left Arm"]

local Rsh = Torso["Right Shoulder"] --Variable for right shoulder
local Lsh = Torso["Left Shoulder"] --Variable for left shoulder

local ArmWeld = Instance.new("Weld")
ArmWeld.Parent = Head
ArmWeld.Part0 = Head
ArmWeld.Part1 = RArm
ArmWeld.C1 = CFrame.new(0,0,0)

local HeadWeld = Instance.new("Weld")
HeadWeld.Parent = Head
HeadWeld.Part0 = Head
HeadWeld.Part1 = LArm
HeadWeld.C1 = CFrame.new(0,0,0)

repeat wait() until Rsh and Lsh --Wait for the left and right shoulder.
Rsh.Part1 = nil --Set weld Part1 to nil.
Lsh.Part1 = nil --Set weld Part1 to nil.

Mouse.Move:connect(function() --Runs the function each time the mouse is moved.
    Mouse.TargetFilter = workspace --This is important when setting the C0 at the bottom as it ignores everything in the workspace to give clean movement of the arms on the Y axis.
    Torso.Neck.C1 = CFrame.new() --Sets the C1 to a blank CFrame so the head doesn't glitch.
    Torso.Neck.C0 = CFrame.new(0, 1.5, 0) * CFrame.Angles(math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y), 0, 0) --Moves the head up by 1.5 studs and then sets the angle of your character's neck looking up towards the mouse using inverse trigonometry (A grade maths) and unit vectors (so you only get the direction.)
end)

Mouse.Idle:connect(function() --Runs the function when the mouse is idle.
    Mouse.TargetFilter = workspace --This is important when setting the C0 at the bottom as it ignores everything in the workspace to give clean movement of the arms on the Y axis.
    Torso.Neck.C1 = CFrame.new() --Sets the C1 to a blank CFrame so the head doesn't glitch.
    Torso.Neck.C0 = CFrame.new(0, 1.5, 0) * CFrame.Angles(math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y), 0, 0) --Moves the head up by 1.5 studs and then sets the angle of your character's neck looking up towards the mouse using inverse trigonometry (A grade maths) and unit vectors (so you only get the direction.)
end)

I hope my answer helped you. If it did, be sure to accept it.

0
Thanks so much! dragonkeeper467 453 — 9y
0
No problem. Spongocardo 1991 — 9y
Ad

Answer this question