Setting an attachment CFrame doesn't work as intended

Hello, I just made a rifle that should shoot with a spread, and to manage bullets tracers I made a particle emitter with an attachment as a parent.
I have made a spreadpart so I can know where the bullet and raycast is actually going to hit, but setting the CFrame of the attachment doesn’t always work and after you spam use the tool the particle gets emitted in totally wrong positions:


The script is the following:

local spread = 1
local tool = script.Parent
local muzzleposition = tool.Front.MuzzlePosition
local tracers = muzzleposition.Attachment.Tracers
local attachment = muzzleposition.Attachment

tool.Shoot.OnServerEvent:Connect(function(plr,hit)
	local spreadpart = game:GetService("ReplicatedStorage").SpreadPart:Clone()
	local distance = ((hit.Position-muzzleposition.Position).Magnitude)/20
	spreadpart.Parent = workspace
	spreadpart.Position = Vector3.new(hit.X + math.random(-spread,spread)*distance,hit.Y + math.random(-spread,spread)*distance,hit.Z + math.random(-spread,spread)*distance)
	local raycast = workspace:Raycast(muzzleposition.Position,(muzzleposition.Position-spreadpart.Position).Unit*1000)
	tool.Front.MuzzlePosition.Sparks:Emit(5)
	attachment.CFrame = CFrame.new(muzzleposition.Attachment.Position,spreadpart.Position)
	tool.LocalBullet:FireClient(plr,tracers)
	tracers:Emit(1)
	wait(5)
	spreadpart:Destroy()
end)

In the local script I just fire the event.
I assumed the problem was that I was giving too many instruction to the attachment, so I already tried to clone it, but the problem is still there.

Edit: I solved the problem (in part) by setting:

attachment.WorldCFrame = CFrame.new(attachment.WorldPosition,spreadpart.Position)

instead of:

attachment.CFrame = CFrame.new(muzzleposition.Attachment.Position,spreadpart.Position)

Now it aproximately shoots the way I want, but I faced another problem.
Basically when I shoot while rotating the character the particle always get emitted in the direction I’m rotating no matter what.
Here’s the video:


It’s not that big of a deal but it annoys me.
Do I have to find another way to emit particles or it is fixable?

Edit 2:
I figured out I just could make a part in the replicated storage which I clone and set the CFrame to the spreadpart, but (who would have guessed) I came across another problem.
Because I have a script that rotates the arm, setting the replicated storage part.Position to the muzzle.Position doesn’t return the effective position in the world of the part:

now the script looks something like this:

local spread = 1
local tool = script.Parent
local muzzleposition = tool.Front.MuzzlePosition

tool.Shoot.OnServerEvent:Connect(function(plr,hit)
	local character = plr.Character
	local tracerpart = game.ReplicatedStorage.Tracerspart:Clone()
	tracerpart.Parent = workspace
	local tracers = tracerpart.Tracers
	local spreadpart = game:GetService("ReplicatedStorage").SpreadPart:Clone()
	local distance = ((hit.Position-muzzleposition.Position).Magnitude)/20
	spreadpart.Parent = workspace
	spreadpart.Position = Vector3.new(hit.X + math.random(-spread,spread)*distance,hit.Y + math.random(-spread,spread)*distance,hit.Z + math.random(-spread,spread)*distance)
	local raycast = workspace:Raycast(muzzleposition.Position,(muzzleposition.Position-spreadpart.Position).Unit*1000)
	tool.Front.MuzzlePosition.Sparks:Emit(5)
	tracerpart.CFrame = CFrame.new(muzzleposition.Position,spreadpart.Position)
	tool.LocalBullet:FireClient(plr,tracers)
	tracers:Emit(1)
	wait(5)
	spreadpart:Destroy()
end)

Final Edit:
I am an idiot.
The script that was making the arms rotate was local.
I made a remote event and a server script and it now works perfectly.

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