Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Creating parts in relation to the position and rotation of another?

I am trying to create a viewmodel for my FPS game. I've just started and already ran into a problem.

I am trying to create two copies of the roblox character's arms, but if the player's torso is rotated in any way, the cloned parts appear in world space, and not in object space (in relation to the torso's current position and rotation)

How do I fix this? I've read up on CFrame:toObjectSpace(), but I am unsure about its usage.

Here is my code:

local character = game.Workspace.Dummy
local fakearms = {}
local welds = {}
local gun = game.ServerStorage.g36
local rarm = Instance.new("Part")
local larm = Instance.new("Part")
rarm.Size = Vector3.new(1,2,1)
rarm.BrickColor = BrickColor.new("Pastel Orange")
larm.Size = Vector3.new(1,2,1)
larm.BrickColor = BrickColor.new("Pastel Orange")
leftweld = Instance.new("Weld",character.HumanoidRootPart)
rightweld = Instance.new("Weld",character.HumanoidRootPart)
welds[1] = rightweld
welds[2] = leftweld
welds[1].Part0 = character.HumanoidRootPart
welds[1].Part1 = rarm
welds[2].Part0 = character.HumanoidRootPart
welds[2].Part1 = larm
rarm.Parent = character
larm.Parent = character
rarm.CFrame = CFrame.new(character.Torso.CFrame.p) * CFrame.new(1.5,0,0)
larm.CFrame = CFrame.new(character.Torso.CFrame.p) * CFrame.new(-1.5,0,0)

2 comments

0

Correct me if I'm wrong, but I think local gun = game.ServerStorage.g36 should be changed to local gun = game.ServerStorage["g36'].

0

That's true but it isn't really relevant to the problem at hand. Thanks anyway, though!

1 answer

✓ Accepted answer

I will explain how to use CFrame:toWorldSpace().

The syntax for the function is as follows:

OriginalCFrame:toWorldSpace(TranslateByThisCFrame)

OriginalCFrame in this case would be Torso.CFrame.

TranslateByThisCFrame would be either CFrame.new(1.5,0,0) or CFrame.new(-1.5,0,0), depending on the arm being translated.

Here is an example of the usage. This translates Left Arm's position -1.5 studs in the x-axis from HumanoidRootPart's position in local space.

local larm = workspace.Player1["Left Arm"]:Clone()
larm.CFrame = workspace.Player1.HumanoidRootPart.CFrame:toWorldSpace(CFrame.new(-1.5,0,0))
larm.Parent = workspace

The same can be done for the right arm, but with a positive 1.5 instead.

1 comment

0

Seeing as you are using Welds, you could also simply change the C0 property of the Weld to (-1.5,0,0) for the left arm weld and (1.5,0,0) for the right arm weld. welds[2].C0 = Vector3.new(-1.5,0,0) welds[1].C0 = Vector3.new(1.5,0,0). Welds use local space as well.

This discussion is preserved as it was. New answers, comments, and votes are closed.