How to make shotgun bullets shoot from the end of the shotgun

Hi, I am a new scripter I was follow a YT tutorial, and it let me down lets say.


as you can see from the screen shot th bullets are coming out of the middle of the gun.

I think I need to send something via remote event but not sure.

game.ReplicatedStorage.ShotEvent2:FireServer(script.Parent.Handle.Position,Mouse.Hit.Position, DamagePerShot, MaxDistance, BulletsPerShot, Spreading)

The position of any object is the central point unless it has been offset. You will need to add a vector3 to adjust the start position of the shells

script.Parent.Handle.Position -- Ideally use CFrame

script.Parent.Handle.CFrame + (script.Handle.CFrame * Vector3.new(0, 0, 0)) -- change vector3 to offset the starting position
1 Like

Hi, Thank you
Do I change X or Z?

It will most likely be the z but have a play with it as look vectors are something you should want to learn later on. (You will be able to visually see the changes)

1 Like

Thank you I will look more into Vectors!

Uh I “tried” to implement but got an error.

output:
ServerScriptService.Gun Bullets:34: invalid argument #1 to ‘new’ (Vector3 expected, got CFrame)

Client:

script.Parent.Activated:Connect(function()
	if Ammo > 0 then
		Ammo = Ammo - 1
		game.ReplicatedStorage.ShotEvent2:FireServer(script.Parent.Handle.CFrame + (script.Parent.Handle.CFrame * Vector3.new(0, 0, 3)),Mouse.Hit.Position, DamagePerShot, MaxDistance, BulletsPerShot, Spreading)
	elseif not Reloading then
		Reload()
	end
end)

Server:

Event2.OnServerEvent:Connect(function(Player, StartPos, TargetPos, Damage, MaxDistance, BulletsPerShot, Spreading)
	for i = 1,BulletsPerShot do
		local Dir = CFrame.new(StartPos,TargetPos)*CFrame.Angles(math.rad(math.random(-Spreading,Spreading)),math.rad(math.random(-Spreading,Spreading)),0)
		local RayCast = Ray.new(StartPos,Dir.LookVector*MaxDistance)
		local Target,Pos = game.Workspace:FindPartOnRay(RayCast, Player.Character,false,true)
		if Target then
			local Hum = Target.parent:FindFirstChild("Humanoid")
			if Hum then
				Hum:TakeDamage(Damage)
			end	
		end
		local Vis = Instance.new("Part",game.Workspace)
		Vis.Anchored = true
		Vis.CanCollide = false
		Vis.BrickColor = BrickColor.new("Gold")
		local Dist = (Pos - StartPos).magnitude
		Vis.CFrame = Dir*CFrame.new(0,0,-Dist)
		Vis.Size = Vector3.new(0.1,0.1,Dist*2)
		game.Debris:AddItem(Vis, 0.3)
	end	
end)

I hope this helps

You could also make the bullets shoot from another part in the tool that’s positioned at the end of the barrel. Currently you are shooting from the handles’ cframe so it would be from the middle of the gun, a second part at the barrel would fire from the front. Of course minimics’s solution of offset will work as well though.

1 Like

Very Smart. I will try and see!

uh, this is more complex then I thought as it involves welds, which I don’t exactly know how to use them

You can use plugins or scripts to easily weld them. There’s a script by Quenty called qperfection weld which could work for your needs. Just put it into the weapon and it should weld all the parts, there are probably better ways but I’ve used it before and it worked fine.

1 Like

Thank you I have made it work!

updating this if anyone wants to know how I did it.

It was easy to do it by creating a part inside the tool and just putting up near the nozzle.

I was curious how to do this the offset way while learning about CFrame.lookat so here is how I did it that way.

I used the handposition, moved up a little to get a more straight line connected through the tool.Handle to the endpoint of the nozzle by using CFrame.lookat. Then just moved the startposition a littlebit in the Z direction to reach the nozzle

local handPosition = toolHandle.Parent.Parent.RightHand.Position + Vector3.new(0,.25,0)
	
local tempCFrame = CFrame.lookAt(handPosition, toolHandle.Position) * CFrame.new(0,0,-2.9)
startPosition = tempCFrame.Position
	
local laserDistance = (startPosition - endPosition).Magnitude
local laserCFrame = CFrame.lookAt(startPosition, endPosition) * CFrame.new(0, 0, -laserDistance/2)