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