Shotgun Spread Issues. (Whats the best way to calculate the spread?)

So im making a shotgun for my game and so far I have been able to create all the 5 bullets, theres one problem. All the bullets follow the same path and I have looked at many posts yet haven’t been able to understand it. Im currently trying to make the spread to create the desired shotgun effect.

Here is my current script:

Tool.Equipped:Connect(function(Mouse)
	Equipped = true
	PlayAnimation:FireServer(ToolAnimations:WaitForChild("Idle"), ToolAnimations:WaitForChild("Idle"):WaitForChild("Speed").Value)

	-- Shooting
	UserInputService.InputBegan:Connect(function(input, gameProccesed)
		if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProccesed and Equipped then

			local mousedown = false
			local Mouse = Player:GetMouse()

			if ToolConfig:WaitForChild("Automatic").Value then
				mousedown = true
			end

			UserInputService.InputEnded:Connect(function(input, gameProccesed)
				if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProccesed and Equipped then
					mousedown = false
				end
			end)

			if Equipped then
				if not Debounce then
					Debounce = true
					repeat

						PlayAnimation:FireServer(ToolAnimations:WaitForChild("Fire"), ToolAnimations:WaitForChild("Fire"):WaitForChild("Speed").Value)
						
						local CurrentPart = nil
						local CurrentPosition = nil
					
						for i=1, ToolConfig.BulletsPerShot.Value do
							
							local ray = Ray.new(Tool:WaitForChild("Handle").CFrame.p, (Mouse.Hit.p - Tool:WaitForChild("Handle").CFrame.p).Unit * 2048 + (Vector3.new(
								math.random(-100,100),
								math.random(-100,100),
								math.random(-100,100)
								)/90))

							local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true)
							CurrentPart = part
							CurrentPosition = position
							
							CastRay:FireServer(ray, position, Tool.Handle.Start.WorldCFrame.Position)	
						end
						
						local beam = Instance.new("Part")
						beam.BrickColor = BrickColor.new("Bright red")
						beam.FormFactor = Enum.FormFactor.Custom
						beam.Material = Enum.Material.Neon
						beam.Transparency = 1
						beam.Anchored = true
						beam.Locked = true
						beam.CanCollide = false

						beam.Parent = workspace

						local distance = (Tool:WaitForChild("Handle").CFrame.p - CurrentPosition).Magnitude
						beam.Size = Vector3.new(0.3, 0.3, distance)
						beam.CFrame = CFrame.new(Tool:WaitForChild("Handle").CFrame.p, CurrentPosition) * CFrame.new(0, 0, -distance / 2)

						if CurrentPart then
							local humanoid = CurrentPart.Parent:FindFirstChildOfClass("Humanoid")

							if not humanoid then
								humanoid = CurrentPart.Parent.Parent:FindFirstChildOfClass("Humanoid")
							end

							if humanoid then
								if CurrentPart.Name == "Head" then
									TakeDamage:FireServer(humanoid, ToolConfig:WaitForChild("Damage").Value * ToolConfig:WaitForChild("HeadshotMultiplier").Value)
									SFX.Headshot:Play()

								else
									TakeDamage:FireServer(humanoid, ToolConfig:WaitForChild("Damage").Value)
									SFX.HitMarker:Play()
								end
							end

							PlaySound:FireServer(Tool:WaitForChild("Handle"):WaitForChild("Fire"))
							Tool:WaitForChild("Handle"):WaitForChild("Fire"):Play()
							task.wait(ToolConfig:WaitForChild("Firerate").Value)
							Debounce = false
							PlayAnimation:FireServer(ToolAnimations:WaitForChild("Idle"), ToolAnimations:WaitForChild("Idle"):WaitForChild("Speed").Value)
							beam:Destroy()
						else

							Tool:WaitForChild("Handle"):WaitForChild("Empty"):Play()
							task.wait(ToolConfig:WaitForChild("Firerate").Value)

							Debounce = false
						end			
						
					until not mousedown
				end
			end
		end
	end)
end)

If you need any other of my scripts, let me know. (server script, entire script)

Mind showing a Video of what Happens?

But a Couple of things

FindPartOnRay is Deprecated, use workspace:Raycast Instead to calculate, it makes it a lot easier, and a lot faster.
The Second thing to is Never Trust the Client, Why are you Raycasting on the Client, when its MUCH Safer to do it on the Server?

Instead of using math.random() to Generate for 3 Different Axis, you can use Random.new() and the Function :NextUnitVector() to Generate a Random Direction.

local rmd = Random.new()

local newV3 = rmd:NextUnitVector() * 1.1
1 Like

Hm, this is definitely new to me as I am a bit stumped on what to do next. Or what to do in general now after reading that.
It’s mainly surprising how the gun script still functioned as usual even though its depracted. Any ideas?

All I’ve managed to do is this segment of rewriting.

						for i=1, ToolConfig.BulletsPerShot.Value do
							
							local RaycastResult = workspace:Raycast(Tool:WaitForChild("Handle").CFrame.p, (Mouse.Hit.p - Tool:WaitForChild("Handle").CFrame.p).Unit * 2048)
							local RayHit = RaycastResult.Instance
							local RayPos = RaycastResult.Position
						
							CastRay:FireServer(ray, position, Tool.Handle.Start.WorldCFrame.Position)	--this line has some red-underlines
						end

Raycasting in this way is pretty new to me and I didn’t know it could be this easy. Now I need to consider the whole of my script as this is pretty overwhelming.

by spread, do you mean something like this?
image

1 Like

Yep. Thats the type of spread I was looking for.

heres the code I used for it

local UIS = game:GetService("UserInputService")

function Createpart()
	local Part = Instance.new("Part")
	Part.Anchored = true
	Part.Size = Vector3.new(1,1,1)
	Part.CanCollide = false
	Part.Color = Color3.new(0.792157, 1, 0.839216)
	Part.Parent = workspace
	return Part
end

local Part45degrees = 8
local Maxparts = 16
local MaxDistance = 10
local Multiplier = 3

local function CreateCircle()
	local Parts = {}
	for i = 1,Maxparts,1 do
		local Origin = script.Parent:WaitForChild("HumanoidRootPart").CFrame
		local Part = Createpart()
		local x = math.cos(math.rad(i) * (45 / ((Maxparts) / Part45degrees)))
		local y = math.sin(math.rad(i) * (45 / ((Maxparts) / Part45degrees)))
		Part.CFrame = Origin * CFrame.new(y * Multiplier,x * Multiplier,0) * CFrame.new(0,0,-MaxDistance)
		table.insert(Parts,Part)
	end
	return Parts
end

UIS.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		local Radials = CreateCircle()
		for I, Part in Radials do
			local NewPart = Createpart()
			NewPart.CFrame = script.Parent:WaitForChild("HumanoidRootPart").CFrame:Lerp(Part.CFrame,0.5)
			NewPart.CFrame = CFrame.lookAt(NewPart.Position,Part.Position)
			NewPart.Size = Vector3.new(0.1,0.1,(script.Parent:WaitForChild("HumanoidRootPart").Position - Part.Position).Magnitude)
		end
	end
end)

feel free to ask me questions about the script

Ill try to find a way to sort of use this for my script. I’m unsure where to start with it though. Im assuming it has to do with pi and all that.

Also the video of my current issue (All 5 bullets are fired in the same direction):

This is the function I used for my bullet generation. I think that may be the issue with it.

CastRay.OnServerEvent:Connect(function(WhoFired, F_Ray, EndPoint, StartPoint)
	FX_Remotes.BigBump:FireClient(WhoFired)
	Tool.Handle.Start.Flash:Emit(1)
	Tool.Handle.Start.Spark:Emit(1)

	local BulletBeam = Instance.new("Part")
	BulletBeam.BrickColor = BrickColor.new("Institutional white")
	BulletBeam.FormFactor = Enum.FormFactor.Custom
	BulletBeam.Material = Enum.Material.Neon
	BulletBeam.Transparency = 0
	BulletBeam.Anchored = true
	BulletBeam.Locked = true
	BulletBeam.CanCollide = false
	BulletBeam.Parent = workspace

	task.spawn(function()
		local Tween = TweenService:Create(BulletBeam, tweenInfo, {Transparency = 1})
		Tween:Play()
		Tween.Completed:Wait()
		BulletBeam:Destroy()
	end)

	local distance = (StartPoint - EndPoint).Magnitude
	BulletBeam.Size = Vector3.new(0.1, 0.1, distance)
	BulletBeam.CFrame = CFrame.new(StartPoint, EndPoint) * CFrame.new(0, 0, -distance / 2)
end)

Heres the shotgun model.

Shotty.rbxm (59.2 KB)

I have made progress to my script! However it still seems to be off and does not come out of the origin point. Any help with this one?

CODE:

	local BulletBeam = Instance.new("Part")
	BulletBeam.BrickColor = BrickColor.new("Institutional white")
	BulletBeam.FormFactor = Enum.FormFactor.Custom
	BulletBeam.Material = Enum.Material.Neon
	BulletBeam.Transparency = 0
	BulletBeam.Anchored = true
	BulletBeam.Locked = true
	BulletBeam.CanCollide = false
	BulletBeam.Parent = workspace

	task.spawn(function()
		local Tween = TweenService:Create(BulletBeam, tweenInfo, {Transparency = 1})
		Tween:Play()
		Tween.Completed:Wait()
		BulletBeam:Destroy()
	end)

	local distance = (StartPoint - EndPoint).Magnitude
	BulletBeam.Size = Vector3.new(0.1, 0.1, distance)
	
	local BulletCFrame = CFrame.new(StartPoint, EndPoint) * CFrame.new(0, 0, -distance / 2)
	BulletBeam.CFrame = BulletCFrame * CFrame.Angles(math.rad(math.random(0,3)), math.rad(math.random(0,3)), math.rad(math.random(0,3)))

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