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
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)
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)
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.
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.
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)