So I am trying to make a shotgun spread with my raycasting gun is there any way I could work this. I have tried already but the gun shoots in a weird way. When I make it shoot 1 bullet it works fine. Any help is appreciated!
Heres the main part of the script:
for ShotNumber = 1, GunInMod[“BulletPerShot”] do
local NewOrigin
if GunInMod[“BulletPerShot”] > 1 then
NewOrigin = Origin + Vector3.new((math.random(-100, 100)/100), (math.random(-100, 100)/100), (math.random(-100, 100)/100))
else
NewOrigin = Origin
end
local Direction = (MousePos - NewOrigin).Unit * 300
local Result = WS:Raycast(NewOrigin, Direction, rParams)
local Intersection = Result and Result.Position or NewOrigin + Direction
local Distance = (NewOrigin - Intersection).Magnitude
local Bullet = ReplicatedStorage.Bullets[GunInMod["BulletType"]]:Clone()
Bullet.Size = Vector3.new(0.1, 0.1, Distance)
Bullet.CFrame = CFrame.new(Origin, (Intersection + NewOrigin)) * CFrame.new(0, 0, -Distance/2)
Bullet.Parent = WS["Bullets/Tracers"]
if Result and Result.Instance.Parent:FindFirstChild("Humanoid") then
local Humanoid = Result.Instance.Parent:FindFirstChild("Humanoid")
if Result.Instance.Name == "Head" then
ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, true)
Humanoid:TakeDamage(GunInMod["Damage"] * GameSettings["GunSettings"]["HeadshotBonus"])
else
ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, false)
Humanoid:TakeDamage(GunInMod["Damage"])
end
end
spawn(function()
task.wait(0.1)
--Bullet:Destroy()
end)
end
I don’t know about your bug but please try this I made can collide false and massless true, also don’t use destroy I made for you debris so I hope it’s gonna work
local Bullet = ReplicatedStorage.Bullets[GunInMod["BulletType"]]:Clone()
Bullet.Size = Vector3.new(0.1, 0.1, Distance)
Bullet.CFrame = CFrame.new(Origin, (Intersection + NewOrigin)) * CFrame.new(0, 0, -Distance/2)
Bullet.Parent = WS["Bullets/Tracers"]
Bullet.CanCollide = false
Bullet.Massless = true
if Result and Result.Instance.Parent:FindFirstChild("Humanoid") then
local Humanoid = Result.Instance.Parent:FindFirstChild("Humanoid")
if Result.Instance.Name == "Head" then
ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, true)
Humanoid:TakeDamage(GunInMod["Damage"] * GameSettings["GunSettings"]["HeadshotBonus"])
else
ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, false)
Humanoid:TakeDamage(GunInMod["Damage"])
end
end
spawn(function()
game.Debris:AddItem(Bullet,0.1)
end)
end
The bullets are already massless and cancollide off. Thanks for letting me know about the debris. Also I have made changes to the script, the bullet works fine but I want to add something which im not sure how to add. The bullets dont actually come out of the gun, so how would I make this?
Heres the new script:
for ShotNumber = 1, GunInMod[“BulletPerShot”] do
local NewOrigin
if GunInMod[“BulletPerShot”] > 1 then
NewOrigin = Origin + Vector3.new((math.random(-100, 100)/100), (math.random(-100, 100)/100), (math.random(-100, 100)/100))
else
NewOrigin = Origin
end
local Direction = (MousePos - NewOrigin).Unit * 300
local Result = WS:Raycast(NewOrigin, Direction, rParams)
local Intersection = Result and Result.Position or NewOrigin + Direction
local Distance = (NewOrigin - Intersection).Magnitude
local Bullet = ReplicatedStorage.Bullets[GunInMod["BulletType"]]:Clone()
Bullet.Size = Vector3.new(0.1, 0.1, Distance)
Bullet.CFrame = CFrame.new(NewOrigin, Intersection) * CFrame.new(0, 0, -Distance/2)
Bullet.Parent = WS["Bullets/Tracers"]
if Result and Result.Instance.Parent:FindFirstChild("Humanoid") then
local Humanoid = Result.Instance.Parent:FindFirstChild("Humanoid")
if Result.Instance.Name == "Head" then
ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, true)
Humanoid:TakeDamage(GunInMod["Damage"] * GameSettings["GunSettings"]["HeadshotBonus"])
else
ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, false)
Humanoid:TakeDamage(GunInMod["Damage"])
end
end
Debris:AddItem(Bullet, 0.1)
end