Attachment Position Doesn't Match Up

I have been working on an FPS framework for a game I plan to make, and recently started making an “customization” system. Specifically, this problem occurs with weapon attachments (like scopes, suppressors, etc). For the sake of not being confusing (mixing the weapon attachment with roblox attachments), lets use the example of a scope.

I add the scope by setting it’s CFrame to the WorldCFrame property of a roblox attachment instance, located in the weapon’s primary part (show below).

I double checked, and the attachment always stays on the primary part and does not move, yet when I set the scope’s CFrame to the attachment’s CFrame it moves somewhere else entirely (shown in the video below).

Here is some additional information that might help “fix” this problem:

  • The system of moving the scope to the attachment’s CFrame works fine when the weapon is unequipped (i.e. the weapon is in its default position and not in the player’s Camera).

Also, here is the code for the part where I move the scope to the attachment:

local attachment = model.PrimaryPart:FindFirstChildOfClass("Attachment");
scope:SetPrimaryPartCFrame(attachment.WorldCFrame);

I was hoping to find a solution to this problem, or at least a reason to why it is happening by posting here, as I have tried for a couple of hours and made no further progress.

A couple questions:

Are you sure the scope has a PrimaryPart? (on a side note it might be a good idea to switch over to scope:PivotTo as SetPrimaryPartCFrame is deprecated)

Are you sure you’re referencing the correct attachment? The part where you said it works fine when the weapon is unequipped makes me wonder if maybe you’re referencing a gun that’s in storage instead of the active equipped one?

If no dice, you might have to try debugging, try printing what the attachment’s WorldCFrame is

Hello,
Sorry for the late response. I was able to fix this issue by using scope:PivotTo(attachment.WorldCFrame). I don’t know why this worked and SetPrimaryPartCFrame didn’t. Sometimes I encounter bugs with roblox studio that doesn’t make sense (maybe it had something to do with the code being defined in a function in a module script). Here is the “working” code for the system in case somebody else needs help to solve a similar issue.

-- Make sure scope is so that all parts within the model are welded to the model's primary part
local scope = <Scope Model Path>:Clone();

-- Model that you want to attach the scope to (in my case its a gun)
local gunModel = <Path To Model>;

-- Roblox Attachment Instance (must be facing the direction you want the scope to face)
local attachment = gunModel.<Path To Where You Want To Place Your Scope (or whatever really)>;

-- Attaching
scope:PivotTo(attachment.WorldCFrame);
scope.Parent = model;

Note: I recommed you organize you code better than this, this is just an example of how you would do it, but making it into a function or something neater that can be reused for multiple models and objects you want to attach is more useful.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.