The bullet from my Viewmodel gun goes in the wrong direction

I want bullets to spawn from the barrel of the Viewmodel gun, which is the visual model the player sees.
However, the bullets are currently spawning from the Tool’s Handle part, not the Viewmodel’s barrel.

I’ve searched through YouTube and the Developer Forum, but I haven’t found a solution to this problem yet.


In this image, red represents the Viewmodel (the gun the player sees), and blue represents the Tool (the actual functional part).
Currently, bullets spawn from the blue part, but I want them to spawn from the red part (the Viewmodel’s muzzle).

I also tried accessing the position of the Viewmodel’s muzzle part in a LocalScript.
It didn’t return nil, but it seems to be giving the wrong position.

I also tried getting the position of the Viewmodel’s muzzle part in a LocalScript, but it returned nil or didn’t work at all.
I looked through YouTube and the Developer Forum but couldn’t find a solution yet.

I tried accessing the position of the Viewmodel’s muzzle part in a LocalScript, it seems to be giving the wrong position

heres my small part of localscript and server script(yea i know those scripts are suck)

local script:

> RemoteFolder.FireGun:FireServer(Mouse.Hit.p, viewmodel.m4a1.Muzzle.MuzzleAttachment.WorldCFrame.Position)
> print(viewmodel.m4a1.Muzzle.MuzzleAttachment.WorldCFrame.Position)

server script:

FireGunEvent.OnServerEvent:Connect(function(player, mouseposition, m4origin)
	if Guncool == false and Mag > 0 then
		Mag -= 1
		Guncool = true
		params.FilterDescendantsInstances = {player.Character}
		localplayer = player

		Muzzle.Fire:Play()

		local randomYaw = math.rad(math.random(-spreadAngle, spreadAngle))
		local randomPitch = math.rad(math.random(-spreadAngle, spreadAngle))
		local spreadCFrame = CFrame.Angles(randomPitch, randomYaw, 0)

		local direction = (mouseposition - m4origin).Unit
		local Origin = m4origin
		print(m4origin)
		print(direction)
		local bulletclone = bullet:Clone()
		bulletclone.Parent = workspace
		VisualizePoint(m4origin, 100)

		local caster = FastCastRedux.new()
		local behaviour = caster.newBehavior()
		behaviour.RaycastParams = params
		local gravity = Vector3.new(0, -workspace.Gravity, 0)
		behaviour.Acceleration = gravity

		caster.LengthChanged:Connect(function(cast, lastpoint, dir, length)
			local blength = bulletclone.Size.Z / 2
			local offset = CFrame.new(0, 0, -(length - blength))
			bulletclone.CFrame = CFrame.lookAt(lastpoint, lastpoint + dir):ToWorldSpace(offset)
		end)

		caster.RayHit:Connect(function(ray, result)
			local hit = result.Instance
			local hitPosition = result.Position
			local hitNormal = result.Normal

			local character = GetCharacterFromHit(hit)
			if character then
				local humanoid = character:FindFirstChildOfClass("Humanoid")
				print(hit, character)
				humanoid:TakeDamage(40)
			else
				CreateImpactEffect(hitPosition, hitNormal, hit)
			end

			
			bulletclone.Anchored = true --(this code added for debugging so nevermind)
		end)

		caster:Fire(m4origin, (mouseposition - m4origin), Bulletspeed, behaviour)

	elseif Mag <= 0 and Reloading == false then
		Reload()
	end

	task.wait(CoolTime_Each_Shot)
	Guncool = false
end)

I’d suggest using this script:

local function VisualizePoint(Point:Vector3,t)
	local Part = Instance.new("Part")
	Part.Parent = workspace.Debug
	Part.Anchored = true
	Part.CanCollide = false
	Part.CanQuery = false
	Part.CanTouch = false
	Part.Position = Point
	Part.Size = Vector3.one*0.5
	Part.Material = Enum.Material.Neon
	Part.Shape = Enum.PartType.Ball
	Part.Color = Color3.new(1,1,1)
	game:GetService("Debris"):AddItem(Part,t or 1)
	return Part
end

To basically check what the viewmodels muzzle position even is. You can run it from the viewmodel handler script and the gun script and check the deviation. That way, it’ll give us a better idea on what scripts at fault.


This is what it looked like when I used the VisualizePoint function on the server.


The red part is where I shot the gun.

have no idea why this happens.

Oh, I forgot to explain that picture. It shows the moment I’m trying to fire a bullet from the barrel of the Viewmodel gun.

I tried shooting while moving sideways, and the X and Z coordinates seem fine, but there seems to be an issue with the Y coordinate.
Every time I equip and unequip the gun, the bullet spawn position on the Y-axis changes significantly.

Hard to figure out what you’re showing us here. Can you like provide a photo with the visualization?

This is my first time using the DevForum, so I was wondering is it okay to share a Google Drive link here?

Yes, it’s okay to share a Google Drive link. It’s just a file. You can also use MediaFire.

This is a video showing both the local and server views. Would this be enough to understand the issue?

You setted it to private.

I’ve requested access though.

Oh sorry i forgot to set it to public.

No worries!

So, these white parts

are meant to represent the muzzles position, correct?

You’re using an attachment for muzzle position correct? Can you check if the attachment is at the correct spot by going to Model > Constraint Details and enabling it?

1 Like

Yes, that’s correct the white parts are meant to represent the muzzle positions.
And yes, I’m using an Attachment for the muzzle.

The attachment is properly positioned.

Anyway, since the white dot and the bullet are both correctly facing the direction I fired in, I don’t think it’s a direction issue it seems more like a problem with the origin.

Oh, I think I found the cause of the issue!
There was another model named Viewmodel inside the Workspace, and it seems like the script was using that one’s origin instead.
Thanks so much for your help I was able to figure it out because of your guidance!

That’s great, it’s solved! No worries. Glad I was able to assist you.

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