I’m sure this is gonna involve CFrame. But I’m not sure how to properly attach the part onto the back of a character? Do I use weld or what? The thing is I want to the part to move the part around the character as well.
Well if you are going to use the term attach then It’s gonna deal with weld. I will give you a example:
local part = workspace.Part local weld = Instance.new("Weld", part) weld.C0 = player.Character.Torso.CFrame * CFrame.new(0,0,2) weld.C1 = part
The C0
and C1
properties of a Weld are used to define offsets respectively from their attached parts, Part0
and Part1
. Also, if you are using R15 character models, there is no part named Torso. A part shared between both R6 and R15 character models is the HumanoidRootPart, so I will use that for example. To attach a Part to a Character’s HumanoidRootPart, you could do something like the following:
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(char)
local torso = char:WaitForChild("HumanoidRootPart") --You can change this to another body part
local newPart = Instance.new("Part", char)
newPart.Massless = true --You will likely want to set at least these properties
newPart.CanCollide = false
newPart.Anchored = false
local weld = Instance.new("Weld", torso) --Could also use a Motor6D, or likewise
weld.C0 = CFrame.new() --Both C0 and C1 are, by default, set to a blank CFrame
weld.C1 = CFrame.new()
weld.Part0 = torso
weld.Part1 = newPart
end)
end)
Would that still work even if I want to rotate the part CFrame? I worry that weld might break?
Will work fine if you want to rotate either of the C0
or C1
properties! You will need to rotate the part by setting these properties though. You wouldn’t be able to just directly set the CFrame of the welded part - that would break the Weld.
Can you tell me how I can rotate the C0 or C1 properties?
Please see the video below of what I’m trying to do (see the white board)
Here’s a link explain CFrame’s better than I could in this short post. From the clip you provided it appears that the Part is simply being rotated (likely via a Tween using the TweenService. Here’s just a little example I coded up. Hopefully it helps!
Put this code is a Script located in either Workspace or ServerScriptService and run the game. It will create a model at (0, 32, 0)
local TweenService = game:GetService("TweenService")
local model = Instance.new("Model", workspace)
local INITIAL_CFRAME = CFrame.new(0,0,-4) * CFrame.Angles(0, 0, math.rad(15))
local FINAL_CFRAME = CFrame.new(0,0,-4) * CFrame.Angles(0, math.rad(180), math.rad(15))
local part0 = Instance.new("Part", model)
part0.Anchored = true
part0.CanCollide = false
part0.Size = Vector3.new(2,2,2)
part0.CFrame = CFrame.new(0,32,0)
local part1 = Instance.new("Part", model)
part1.Anchored = false
part1.CanCollide = false
part1.Size = Vector3.new(4,4,0.2)
local weld = Instance.new("Weld", part0)
weld.Part0 = part0
weld.Part1 = part1
weld.C0 = INITIAL_CFRAME
local spinHalf_TweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local tween = TweenService:Create(
weld,
spinHalf_TweenInfo,
{
C0 = FINAL_CFRAME
}
)
while true do
tween:Play()
wait(spinHalf_TweenInfo.Time + 2)
weld.C0 = INITIAL_CFRAME
end
I tested it and it looks promising. The thing is when it rotates, the part’s position need to move forward a little bit (so it appears in front of the character’s torso, instead of staying at the back). How can I do this?
The way I set up my code you can just alter the variable FINAL_CFRAME
. To move an object forward you need to decrease (I know, its a counter-intuitive) the Z-component. Try changing it to this and you can alter the Z component of the CFrame as you see fit:
local FINAL_CFRAME = CFrame.new(0,0,-6) * CFrame.Angles(0, math.rad(180), math.rad(15))