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

What is a good approach to allow rotation of welded, moving parts on players?

Asked by 8 years ago

I've been working on developing a script without using preset animations, but came across some trouble recently on trying to get the left arm of the player to rotate systematically (custom) while the player is walking (only when the mouse is held down). Below is my attempt to solve it, and following that is the original code I had:

wait(1)
q= 1

while q==1 do
    local Player = game.Players.LocalPlayer
    local Mouse = Player:GetMouse()
    if game.Players.LocalPlayer.Character.LeftArmClone then
        fg= game.Players.LocalPlayer.Character.LeftArmClone
        for i=1,100 do
            fg.CFrame= fg.CFrame*CFrame.Angles(0,0,0.1)*CFrame.new(0.05,0,0)
            wait(0)
        end
        wait(0.2)
    end
    wait(0)
end

Here, this script was inserted under a model in the Workspace, and its goal was to try to CFrame a rotation of the brick by steps.fg.CFrame= fg.CFrame*CFrame.Angles(0,0,0.1)*CFrame.new(0.05,0,0)represents the original position, the angle of rotation, and some translation to prevent the part from sliding off, respectively. This was influenced by a previously successful script for an NPC model I used before.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

--creates a weld to join the animated arm to the torso
local function weldBetween(a,b)
    local weld= Instance.new("ManualWeld",a)
    weld.Part0= a
    weld.Part1= b
    weld.C0= a.CFrame:inverse()*b.CFrame
    return weld
end

local vb= 0--extra variable for testing purposes
local holdVal= 0
ResetValue= function(Self)
    holdVal= 0
end

--this acts also like a hold function, the value increases as the left button is held down
Mouse.Button1Down:connect(function()

    local leftArmClone= game.Players.LocalPlayer.Character["Left Arm"]:Clone()
    leftArmClone.Parent= game.Players.LocalPlayer.Character
    leftArmClone.Name= "LeftArmClone"
    leftArmClone.Anchored= true
    leftArmClone.TopSurface= "Smooth"
    leftArmClone.BottomSurface= "Smooth"
    leftArmClone.FrontSurface= "Smooth"
    leftArmClone.BackSurface= "Smooth"
    leftArmClone.LeftSurface= "Smooth"
    leftArmClone.RightSurface= "Smooth"
    --game.Players.LocalPlayer.Character["Right Arm"].TopSurface= "Weld"
    weldBetween(game.Players.LocalPlayer.Character.Torso,leftArmClone)
    local kl= game.Players.LocalPlayer.Character["Left Arm"]
    leftArmClone.Position= Vector3.new(kl.Position.X,kl.Position.Y,kl.Position.Z)
    leftArmClone.Anchored= false

    --]]
    --[[
--another attempt by changing the surface to get a desired weld type, didn't work
    local leftArmClone= game.Players.LocalPlayer.Character["Left Arm"]:Clone()
    leftArmClone.Parent= game.Players.LocalPlayer.Character
    leftArmClone.Name= "LeftArmClone"
    leftArmClone.Anchored= true
    leftArmClone.TopSurface= "SmoothNoOutlines"
    leftArmClone.BottomSurface= "SmoothNoOutlines"
    leftArmClone.FrontSurface= "SmoothNoOutlines"
    leftArmClone.BackSurface= "SmoothNoOutlines"
    leftArmClone.LeftSurface= "SmoothNoOutlines"
    leftArmClone.RightSurface= "SmoothNoOutlines"
    --]]

--[[    
    --in-script left arm animation; haven't gotten to work; it is modeled off NPC left arm animation
    local fg= game.Players.LocalPlayer.Character.LeftArmClone
    if game.Players.LocalPlayer.Character.LeftArmClone then
        fg.CFrame= fg.CFrame*CFrame.Angles(0,0,0.1)*CFrame.new(0.05,0,0)
        wait(0)
    end
--]]

    Mouse.Button1Up:connect(function()--inside button down so holdVal can recount if pos changes
        holdSwitch= 1
        ResetValue()
        if game.Players.LocalPlayer.Character:FindFirstChild("LeftArmClone") then
            leftArmClone:Destroy()
        end
    end)

    holdSwitch= 0
    while holdSwitch== 0 do
        wait(0)
        holdVal= holdVal+1
        print(holdVal)
        game.Players.LocalPlayer.Character["Left Arm"].Transparency= 1  
        ----------------

        ----------------------- 
        if holdVal==1 then
            game.Players.LocalPlayer.Character["Left Arm"].Transparency= 0
        end
    end
end)

The script above is the original script I have. Basically, the left arm is cloned, beginning at local leftArmClone= game.Players.LocalPlayer.Character["Left Arm"]:Clone(). Next, the surfaces of the cloned arm is changed to smooth, a function to weld the left arm to the torso is instantiated, the left arm is shifted to prevent overlap with the torso, and finally the part becomes un-anchored as the left arm clone won't fall off. If the mouse button is released, the original arm reappears, in:

if holdVal==1 then
            game.Players.LocalPlayer.Character["Left Arm"].Transparency= 0
        end

Because, when the right mouse button is released the value of holdVal is 1, not 0.

For a supplement just to help with my question/ get an idea, this was my left arm NPC animation script that worked just because it only had to translate in a fixed single direction and velocity while rotating. What this one does is that it rocks the arm back and forth in a swinging motion, non-stop as the server is running:

while true do   
    local pos1= workspace.NPC.LeftArm.Position.Y
    --print("Pos 1: "..workspace.NPC.LeftArm.Position.Y)
    for i=1,7 do
        workspace.NPC.LeftArm.CFrame= workspace.NPC.LeftArm.CFrame*CFrame.Angles(0,0,0.1)*CFrame.new(0.05,0,0)
        wait(0)
    end
    wait(0.2)
    for i=1,7 do
        workspace.NPC.LeftArm.CFrame= workspace.NPC.LeftArm.CFrame*CFrame.Angles(0,0,-0.1)*CFrame.new(-0.05,0,0)
        wait(0)
    end
    for i=1,7 do
        workspace.NPC.LeftArm.CFrame= workspace.NPC.LeftArm.CFrame*CFrame.Angles(0,0,-0.1)*CFrame.new(-0.05,0,0)
        wait(0)
    end
    wait(0.2)
    for i=1,7 do
        workspace.NPC.LeftArm.CFrame= workspace.NPC.LeftArm.CFrame*CFrame.Angles(0,0,0.1)*CFrame.new(0.05,0,0)
        wait(0)
    end
    --workspace.NPC.LeftArm.CFrame= workspace.NPC.LeftArm.CFrame*CFrame.new(0,-0.05,0)
    local pos2= workspace.NPC.LeftArm.Position.Y
    local pos3= pos2-pos1--positive
    --print("Pos 2: "..workspace.NPC.LeftArm.Position.Y)
    workspace.NPC.LeftArm.CFrame= workspace.NPC.LeftArm.CFrame*CFrame.new(0,-pos3,0)--if too high should go down, so '-' sign
end

Referring back to the original code, when the left mouse is held down the left arm clone becomes frozen (no rotation) at the last site where the original left arm became transparent, but translates well with the player and the player's faced direction. In advance, thanks for taking a look at this; I will follow up with any inquiries.

Answer this question