Gun spray pattern not going like expected

  1. What do you want to achieve? Keep it simple and clear!
    I wanted to make the spray pattern to looks like counter strike ak47 spray pattern

  2. What is the issue? Include screenshots / videos if possible!

It’s not going to the pattern that I expected it to looks like.
3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried adjusting the spray pattern position but it does not work.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

ServerScript (inside tool)

local shotCount = 0

function fire(player,mousePos)

	if ammoInMag > 0 and not reloading then
		
		local startPos = handle.Muzzle.WorldPosition
		local velocity = 1650
		
		shotCount = math.min(shotCount + 1, #module.sprayPattern)
		local pattern = module.sprayPattern[shotCount]
		local adjustedDirection = CFrame.new(startPos, mousePos) * CFrame.Angles(0, math.rad(pattern.x), math.rad(pattern.y))
		
		local direction = CFrame.new(startPos,mousePos)
		direction = direction.lookVector*velocity
		local range = 3000
		
		canFire = true
		
		local fireSound = tool.Handle.Sound:WaitForChild("Fire")
		fireSound:Play()
		
		local mFlash = tool:WaitForChild("Flash"):Clone()

		mFlash.Parent = tool.Handle.Muzzle
		wait(.2)
		
		if mFlash.Parent == tool.Handle.Muzzle then
			mFlash:Destroy()
		end
		
		--[[
		fireSound.Ended:Connect(function()
			fireSound:Destroy()
		end)
		--]]
		
		bulletReplicator:FireAllClients(player.Character,startPos,direction,range)
		
		if ammoInMag > 0 then ammoInMag -= 1 end

		updateClientAmmo(player, ammoInMag, ammoStored)
		print("Ammo"..ammoInMag)
		
		local startTime = tick()
		local lastPos = startPos
		local blacklist = {player.Character}
		local rayFilter = RaycastParams.new()
		rayFilter.FilterType = Enum.RaycastFilterType.Exclude
		for _,char in pairs(workspace:GetChildren()) do
			if char:FindFirstChildOfClass("Humanoid") then
				for _,accessory in pairs(char:GetChildren()) do
					if accessory:IsA("Accessory") then
						table.insert(blacklist,accessory.Handle)
					end
				end
			end
		end

		rayFilter.FilterDescendantsInstances = blacklist

		while range > 0 do
			local timeLength = (tick()-startTime)

			local currentPos = startPos + (direction*timeLength)

			local distance = (lastPos-currentPos).Magnitude
			range -= distance

			local ray = workspace:Raycast(lastPos,currentPos-lastPos,rayFilter)
			if ray == nil then
				ray = workspace:Raycast(currentPos,lastPos-currentPos,rayFilter)
			end
			if ray then
				local hit = ray.Instance
				currentPos = ray.Position
				local model = hit:FindFirstAncestorOfClass("Model")
				if model then
					local humanoid = model:FindFirstChildWhichIsA("Humanoid")
					if humanoid then
						
						if hit.Name == "Head" then
							humanoid:TakeDamage(bodyDamage * headDamMultiplier)
						else
							humanoid:TakeDamage(bodyDamage)
						end
						print("Target HP "..humanoid.Health)
					end
				end
				
				local newHole = bulletHole:Clone()
				newHole.CFrame = CFrame.new(ray.Position, ray.Position + ray.Normal)
				newHole.Parent = workspace
				Debris:AddItem(newHole, 5)  -- Automatically clean up the bullet hole after 5 seconds
				
				break
			end
			lastPos = currentPos
			task.wait()
			
		end

end

The spray patterns in Module Script

	sprayPattern = {
		{x = 0, y = 5},   
		{x = -3, y = 2},  
		{x = -5, y = 1},
		{x = 1, y = 1},
		{x = 5, y = 0},   
		{x = 6, y = -0.5},
		{x = -5, y = 0},
		{x = -6, y = 0},
		{x = 2, y = 0},
		{x = 5, y = 0},
		{x = 0, y = -2},
		{x = -3, y = 3},
		{x = 4, y = 2},
	}

Sorry for not cleaning up the codes for uneccessary things beside the spraying pattern. So glad if you help!

1 Like

you should move the camera, after each shot offset its rotation it by that amount

Hey, I dont really get what you mean but can moving the camera make the bullet goes like the pattern above?

ofcourse since its used for recoil and its would be something like this:

Camera.CFrame *= CFrame.Angles( xAngle , yAngle, zAngle offsets )

you only need x and y for mocing camera left right and up down repectively

Tks, it works. But how to make the spraying smoothier. Can Lerp make the camera smooth?

yeah but you can use tween also

how do I use a tween too? I’m pretty new at it

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