I’m trying to make a shotgun spread, but it doesn’t work as intended. Here’s a video: https://streamable.com/82zgmp
and here’s my code:
local Shotgun = script.Parent
local BulletShooter = script.Parent:WaitForChild("BulletShooter")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local MaxOffset = script:WaitForChild("MaxOffset").Value
local Bullet = script:WaitForChild("Bullet")
local BulletsPerShot = script:WaitForChild("BulletsPerShot").Value
local function activated()
local RandomN = Random.new()
for i = 1, BulletsPerShot, 1 do
local direction = Mouse.Hit.LookVector * 80
local Offset = Vector3.new(RandomN:NextNumber(-MaxOffset, MaxOffset), RandomN:NextNumber(-MaxOffset, MaxOffset), RandomN:NextNumber(-MaxOffset, MaxOffset))
local hit, position = game.Workspace:FindPartOnRay(Ray.new(BulletShooter.Position, (Mouse.Hit.LookVector + Offset) * 300))
local direction = position
local bullet = Bullet:Clone()
Bullet.CFrame = BulletShooter.CFrame + Vector3.new(0, 0, 1)
local Velocity = Instance.new("BodyVelocity")
Velocity.MaxForce = Vector3.new(1, 1, 1) * math.huge
Velocity.Velocity = direction
Velocity.Parent = bullet
bullet.Parent = game.Workspace
wait(5)
Bullet:Destroy()
end
end
Shotgun.Activated:Connect(activated)
Seems like the issue is here as wait yields and prevents the other bullets from being fired. To prevent it from yielding the entire script use debris service to destroy the bullet after 5 seconds
local Shotgun = script.Parent
local BulletShooter = script.Parent:WaitForChild("BulletShooter")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local MaxOffset = script:WaitForChild("MaxOffset").Value
local Bullet = script:WaitForChild("Bullet")
local BulletsPerShot = script:WaitForChild("BulletsPerShot").Value
local function activated()
local RandomN = Random.new()
for i = 1, BulletsPerShot, 1 do
local direction = Mouse.Hit.LookVector * 80
local OffsetVector = Vector3.new(RandomN:NextNumber(-MaxOffset, MaxOffset), RandomN:NextNumber(-MaxOffset, MaxOffset), RandomN:NextNumber(-MaxOffset, MaxOffset))
local hit, position, normal = game.Workspace:FindPartOnRay(Ray.new(BulletShooter.Position, (Mouse.Hit.LookVector + OffsetVector) * 300))
local direction = position
local bullet = Bullet:Clone()
Bullet.CFrame = BulletShooter.CFrame
local Velocity = Instance.new("BodyVelocity")
Velocity.MaxForce = Vector3.new(1, 1, 1) * math.huge
Velocity.Velocity = direction
Velocity.Parent = bullet
bullet.Parent = game.Workspace
game:GetService("Debris"):AddItem(bullet, 3)
end
end
Shotgun.Activated:Connect(activated)
Uhh, use this formula by Quasiduck which uses a CFrame to generate an x,y-axis to place the points over a disk which is what I believe the “realistic” feeling you are looking for: