Gun spread not working properly

Hello, this is my first topic post in the DevForum, im quite new and don’t really know much of it.

So, i’m not really good at scripting, however, i can make some decent things. Today i was making this “fish gun” item for one of my admin games, and it has a “triple” mode which fires 3 bullets with spread. However, aiming at the sky doesn’t make the bullets spread, but if you aim at a close object, the spread works.

The small neon spheres are the size of the bullets, they are visible for testing.

gun spread not working.wmv (4.7 MB)

local body = 5
local shoot = script.Parent.Shoot
local hold = script.Parent.Hold
local cooldown = false
local ammo = 100
local max = 100
local human = nil
local equipped = false
local recoil = 230
local bullets = 3
local cmode = 1
local gui = script.Parent.water
local holding = nil

function dealdmg(target)
	target:TakeDamage(body)
	local slowdown = game.ServerStorage.Dummy.Fight.slowdown:Clone()
	if target.WalkSpeed > 0 then
		slowdown.slow.Value = 1
		slowdown.Parent = target.Parent
		slowdown.Enabled = true
	end
end

script.Parent.Fire.OnServerEvent:Connect(function(player, input, pos)
	if input == "change" then
		script.Parent.Handle.changed:Play()
		if cmode == 1 then
			cmode = 2
			body = 30
			gui.mode.Text = "Mode: Big"
		elseif cmode == 2 then
			body = 8
			cmode = 3
			gui.mode.Text = "Mode: Triple"
		elseif cmode == 3 then
			body = 5
			cmode = 1
			gui.mode.Text = "Mode: Normal"
		end
	end
	if input == "taunt" and cooldown == false then
		cooldown = true
		local rng = math.random(1, 2)
		if rng == 1 then
			script.Parent.Handle.augh:Play()
			local chat = game:GetService("Chat")
			local anim = script.Parent.Parent:FindFirstChildOfClass("Humanoid"):LoadAnimation(shoot)
			anim:Play()
			chat:Chat(script.Parent.Handle, "augh", Enum.ChatColor.Blue)
			task.wait(script.Parent.Handle.augh.TimeLength)
			cooldown = false
		end
		if rng == 2 then
			script.Parent.Handle.taunt:Play()
			pcall(function()
				local chat = game:GetService("Chat")
				local anim = script.Parent.Parent:FindFirstChildOfClass("Humanoid"):LoadAnimation(shoot)
				anim:Play()
				chat:Chat(script.Parent.Handle, "hello", Enum.ChatColor.Blue)
				task.wait(0.7)
				anim:Play()
				chat:Chat(script.Parent.Handle, "how are u", Enum.ChatColor.Blue)
				task.wait(0.9)
				anim:Play()
				chat:Chat(script.Parent.Handle, "im under da water", Enum.ChatColor.Blue)
				task.wait(1.3)
				anim:Play()
				chat:Chat(script.Parent.Handle, "plz help me", Enum.ChatColor.Blue)
				task.wait(1.2)
				anim:Play()
				chat:Chat(script.Parent.Handle, "yourtoomindof woooooouuuu", Enum.ChatColor.Blue)
				task.wait(1.5)
			end)
			cooldown = false
		end
	end
	if input == "begin" and equipped == true then
		if ammo <= 0 and cooldown == false then
			cooldown = true
			local anim = script.Parent.Parent:FindFirstChildOfClass("Humanoid"):LoadAnimation(script.Parent.Reload)
			if holding ~= nil then
				holding:Stop()
			end
			anim:Play()
			script.Parent.Handle.Reload:Play()
			anim.Stopped:Wait()
			cooldown = false
			if equipped == true then
				holding:Play()
			end
			ammo = max
		elseif cooldown == false and ammo > 0 then
			cooldown = true
			script.Parent.Handle.augh:Play()
			if cmode == 1 then
				if ammo > 0 then
					ammo -= 1
					local anime = script.Parent.Parent:FindFirstChildOfClass("Humanoid"):LoadAnimation(shoot)
					anime:Play()
					local water = Instance.new("Part")
					water.Name = "projectile"
					water.Size = script.Parent.Effect.Size
					water.Transparency = 1
					water.Shape = "Ball"
					water.Material = "Neon"
					water.Color = Color3.new(0, 1, 1)
					water.CanCollide = false
					water.Anchored = false
					water.CFrame = script.Parent.Effect.CFrame
					water.Velocity = (pos - water.Position).unit * 200
					local trail = script.Parent.Handle.Trail:Clone()
					trail.Parent = water
					local attach = Instance.new("Attachment", water)
					attach.CFrame = attach.CFrame * CFrame.new(0, -0.2, 0)
					local attach2 = Instance.new("Attachment", water)
					attach2.CFrame = attach2.CFrame * CFrame.new(0, 0.2, 0)
					trail.Attachment0 = attach
					trail.Enabled = true
					trail.Attachment1 = attach2
					local sfx = script.Parent.Handle.water:Clone()
					sfx.Parent = water
					sfx:Play()
					local sfx2 = script.Parent.Handle.sfxhit:Clone()
					sfx2.Parent = water
					local particle = script.Parent.Handle.hit:Clone()
					particle.Parent = water
					water.Parent = workspace
					game:GetService("Debris"):AddItem(water, 10)
					local touched = false
					water.Touched:Connect(function(hit)
						local pos = water.Position
						if not hit:IsDescendantOf(script.Parent.Parent) and touched == false and hit.Name ~= "projectile" then
							touched = true
							sfx:Destroy()
							water.Anchored = true
							trail.Enabled = false
							particle:Emit(30)
							sfx2:Play()
							game:FindService("Debris"):AddItem(water, 3)
							if hit.Parent:FindFirstChildOfClass("Humanoid") or hit.Parent.Parent:FindFirstChildOfClass("Humanoid") then
								if hit.Parent:FindFirstChildOfClass("Humanoid") then
									dealdmg(hit.Parent:FindFirstChildOfClass("Humanoid"))
								end
								if hit.Parent.Parent:FindFirstChildOfClass("Humanoid") then
									dealdmg(hit.Parent.Parent:FindFirstChildOfClass("Humanoid"))
								end
							end
						end
					end)
				end
			end
			if cmode == 3 then
				for i = 1, bullets do
					if ammo > 0 then
						ammo -= 1
						local anime = script.Parent.Parent:FindFirstChildOfClass("Humanoid"):LoadAnimation(shoot)
						anime:Play()
						local water = Instance.new("Part")
						water.Name = "projectile"
						water.Size = script.Parent.Effect.Size
						water.Transparency = 0
						water.Material = "Neon"
						water.Color = Color3.new(0, 1, 1)
						water.Shape = "Ball"
						water.CanCollide = false
						water.Anchored = false
						water.CFrame = script.Parent.Effect.CFrame
--problem here--
						local newpos = pos + Vector3.new(math.random(-5, 5),math.random(-5, 5),math.random(-5, 5))
						water.Velocity = (newpos - water.Position).unit * 200
--end of problem here--
						local trail = script.Parent.Handle.Trail:Clone()
						trail.Parent = water
						local attach = Instance.new("Attachment", water)
						attach.CFrame = attach.CFrame * CFrame.new(0, -0.2, 0)
						local attach2 = Instance.new("Attachment", water)
						attach2.CFrame = attach2.CFrame * CFrame.new(0, 0.2, 0)
						trail.Attachment0 = attach
						trail.Enabled = true
						trail.Attachment1 = attach2
						local sfx = script.Parent.Handle.water:Clone()
						sfx.Parent = water
						sfx:Play()
						local sfx2 = script.Parent.Handle.sfxhit:Clone()
						sfx2.Parent = water
						local particle = script.Parent.Handle.hit:Clone()
						particle.Parent = water
						water.Parent = workspace
						game:GetService("Debris"):AddItem(water, 10)
						local touched = false
						water.Touched:Connect(function(hit)
							local pos = water.Position
							if not hit:IsDescendantOf(script.Parent.Parent) and touched == false and hit.Name ~= "projectile" then
								touched = true
								sfx:Destroy()
								water.Anchored = true
								trail.Enabled = false
								particle:Emit(30)
								sfx2:Play()
								game:FindService("Debris"):AddItem(water, 3)
								if hit.Parent:FindFirstChildOfClass("Humanoid") or hit.Parent.Parent:FindFirstChildOfClass("Humanoid") then
									if hit.Parent:FindFirstChildOfClass("Humanoid") then
										dealdmg(hit.Parent:FindFirstChildOfClass("Humanoid"))
									end
									if hit.Parent.Parent:FindFirstChildOfClass("Humanoid") then
										dealdmg(hit.Parent.Parent:FindFirstChildOfClass("Humanoid"))
									end
								end
							end
						end)
					end
				end
			end
			if cmode == 1 or cmode == 3 then
				task.wait(0.05)
			end
			cooldown = false
		end
	end
end)

script.Parent.Equipped:Connect(function()
	equipped = true
	gui.Parent = game.Players[script.Parent.Parent.Name].PlayerGui
	human = script.Parent.Parent:FindFirstChildOfClass("Humanoid")
	script.Parent.Client.Enabled = true
	holding = script.Parent.Parent:FindFirstChildOfClass("Humanoid"):LoadAnimation(hold)
	holding.Looped = true
	holding:Play()
	while equipped == true do
		task.wait()
		game:GetService("TweenService"):Create(gui.water, TweenInfo.new(0.2, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 0, false), {Size = UDim2.new(0.268 * ammo / max, 0, 0.08, 0)}):Play()
	end
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	gui.Parent = script.Parent
	human = nil
	script.Parent.Client.Enabled = false
	holding.Looped = false
	holding:Stop()
end)

I’ve added a comment of where the issue could be with the spread.
Any idea?

2 Likes

Just a few things about posting here.
Don’t use the term ‘bug’ in your tags since that relates to problems with Studio or the website.

Next when you copy/paste a script here you can use 3 backticks before and after the script to properly format it and make it look like code in Studio.

Please post only the section(s) of code dealing with aiming and the spread. I tried CTRL F to find the word spread in your pages of script and gave up looking because I couldn’t find a variable named spread.

1 Like

Thank you, i have anxiety issues with people replying.

1 Like

I also had very little luck finding the spread section in the code. But, since this script appears to missing CFrame.Angles or any other rotation based cframe … I could assume:

  • You meant to change the angle of the spread, but instead changed the position.
  • You are using the spread by offsetting the target position to the left or right

When using spread, I would do something like this:

local BulletCFrame = CFrame.new(bullet.Position,Target.Position)
local Spread = 5 -- degrees
BulletPart.CFrame = BulletCFrame.Position * CFrame.Angles(0,math.rad(5),0)

Obviously, not everything is correct. But it is pretty close.

I’ve made a shotgun with spread before, but yeah, i didn’t remember i could use CFrame.Angles.

All i did was change the lines after the comment i put.

water.CFrame = CFrame.lookAt(water.Position, pos) * CFrame.Angles(math.rad(math.random(-5, 5)),math.rad(math.random(-5, 5)),math.rad(math.random(-5, 5)))
water.Velocity = water.CFrame.LookVector * 200
2 Likes

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