Trying to Place Attachment 7 Studs In Front of Player

Basically, I’m trying to use the function :ToObjectSpace so the attachment can get put in the right place in the workspace, but I’m not sure how to also add the way the humanoid is looking and 7 studs in front, any help please?

CODE

local attachment = Instance.new("Attachment")
attachment.CFrame = humrp.CFrame:ToObjectSpace(humrp.CFrame.lookVector * 7)
1 Like

Are you able to do

local attachment = Instance.new("Attachment")
attachment.CFrame = humrp.CFrame * CFrame.new(0, 0, -7) --Negative on the Z-Axis is forward

Hey there, attachements already work off of object space with their “position” parameter. The attachements rotate with the parented part.

Ex: attachment.Position = Vector3.new(0,0,7)

Hope this helps!

Alright, I’ll try this eventually, thanks!

Hmm, I tried just doing it using the lookvector of the humanoid * 7 but it still didnt work and it put the attachment no where near where it was supposed to be at

There is no lookvector, It’s just Vector3.new(0,0,7).

Roblox character attachements work off of positional offsets, still turning with the part it is parented to.

Edit: Please read this: Attachment | Documentation - Roblox Creator Hub

It needs to be negative on the Z Axis. Positive is backwards. Try my solution or Hidden’s

local offset = 7 -- changeable

local attachment = Instance.new("Attachment")
attachment.CFrame = CFrame.new(0, 0, -(humrp.Size.Z/2 + offset))

Attachments work differently, I usually think of it as ignoring the CFrame you first multiply with if you are multiplying from its parent which in this case you would be if it was a part. Usually if this wasn’t an attachment it would be:

humrp.CFrame * CFrame.new(0, 0, -(humrp.Size.Z/2 + offset))

But with the attachment, you have to do it like this:

CFrame.new(0, 0, -(humrp.Size.Z/2 + offset))

Negative Z direction for forward by the way so thats where the negative sign comes in.

Size.Z/2 is to offset to get to the surface of the HumanoidRootPart from the forward direction then add your offset studs.

That doesn’t seem to work here’s what happens

It’s not placing in the right place at all

Could you take a look at my method?
Attachment.Position
Reading that shows how to do it.

There is no lookvector, It’s just Vector3.new(0,0,7).

Roblox character attachements work off of positional offsets, still turning with the part it is parented to.

I knew that already, in this case I had this same problem and had to use ToObjectSpace, also your potential solution also didnt work, good try though! :call_me_hand:

1 Like

Hold on, what are you trying to do then?

It says you’re trying to put it 7 studs in front of the player.
Am I missing something?

Oh, I’m guessing it has to be in workspace or is that not so?

I am trying to make the attachment position itself 7 studs in front of the player’s humanoid root part, onto the part, I’m using raycasting to find the part in front of the player 7 studs.

Sorry, I thought it could be anywhere. My solution was for when the attachment was in the humanoid root part like so.

1 Like

Wow, that is amazing, thanks for trying

I don’t want to waste any more of your time, but could I ask why it needs to be inside the workspace? The only reason I could think of is you’re trying to use world position (solved by attachment.WorldPosition) for an endpoint while raycasting.

Yeah basically I just want the attachment to go to where the ray ends which is 7 studs in front of the player

So if you do have a loop, you can use just use set the attachement position inside of the humanoidrootpart to the negative magnitude between the humanoidrootpart and end point

Uh alright Ill see what I can do, thanks!

I got this to work easily, heres a video of it and my source code. I didn’t explain it the best so iI thought i’d just show you instead.
https://youtu.be/aEqpSKIsxO4

local plr = game.Players.LocalPlayer

local char = plr.Character
local hrp = char.HumanoidRootPart

local attach = Instance.new("Attachment")
attach.Visible = true
attach.Parent = hrp
local maxPos = 7

local function raycast()
	local ray = Ray.new(hrp.Position, hrp.CFrame.LookVector * maxPos)
	return workspace:FindPartOnRay(ray, char)
end


game:GetService("RunService").RenderStepped:Connect(function()
	local part,pos,surface = raycast()
	local mag = (pos - hrp.Position).Magnitude
	attach.Position = Vector3.new(0,0,-mag)
end)