Need help with shooting script reflecting bullets

So basically I have this gun system and it works fine but when you got up close to something and start shooting the bullets come out but then spread in random directions and just feels sloppy as you can hit anyone not on purpose because you just came close to them and I’m no good gun scripter and just took this from a project with me and my friend like a year ago and he did the main shoot part but I lost contact of him now.

function Fire(MousePos)
	local BarrelPos = Barrel.Position
	
	for _,v in pairs(Barrel:GetChildren()) do
		if v:IsA('ParticleEmitter') then
			v.Enabled = true
		end
	end
	
	delay(.02,function()
		for _,v in pairs(Barrel:GetChildren()) do
			if v:IsA('ParticleEmitter') then
				v.Enabled = false
			end
		end
	end)
	local mPos = ReturnPosFromRay(MousePos) + Vector3.new(math.random(-Spread, Spread),math.random(-Spread, Spread),math.random(-Spread, Spread))
	local B = BulletBase:clone()
	if IsLaser then
		B.ParticleEmitter.Color = ColorSequence.new(RayColor)
		B.ParticleEmitter:Emit(100)
	end
	
	local StartCF = CFrame.new(BarrelPos, mPos) * CFrame.new(0, 0, -2)
	B.CFrame = StartCF
	B.Parent = RayStorageModel
	
	for _, light in pairs(Barrel:GetChildren()) do
		if light:IsA("Light") then
			Flash(light)
		end
	end
	
	if FireSound then
		SoundEvent:FireServer(FireSound)
	end
	RenderEvent:FireServer{BarrelPos, mPos, SettingsModule, IgnoreTable}
	
	spawn(function()
		recoil = 10
		local LastStep = BarrelPos
		local DistanceTravelled = 0
		while B and DistanceTravelled < 150 do
			local Delta = RS:wait()
			local CurDistanceTravelled = Delta * Speed
			B.CFrame = B.CFrame * CFrame.new(0, 0, -DistanceTravelled) * (IsLaser and CFrame.Angles(0, 0, 0) or CFrame.Angles(math.rad(-.1), 0, 0))
			local part, position, surfacenormal = workspace:FindPartOnRayWithIgnoreList(Ray.new(LastStep, (B.Position - LastStep)), IgnoreTable)
			if part then
					local Humanoid = RecursiveFindHumanoid(part)
					if Humanoid then
						if Humanoid.Parent ~= part.Parent then
							if not IsLaser then
							end
						end
						local OtherPlayer = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
						if TKEnabled or (OtherPlayer and OtherPlayer.TeamColor ~= Player.TeamColor or Player.Neutral) or not OtherPlayer then
							DamageEvent:FireServer(Humanoid, BaseDamage)
						end
					
					break
				end
			end
			DistanceTravelled = DistanceTravelled + CurDistanceTravelled
			LastStep = B.Position
		end
		B:Destroy()
	end) 
	end

https://gyazo.com/d757bb3f68081ef199e293fbab9d81dd

Any help will be useful :smile:

1 Like