In my game, I have attempted to create a shotgun that will shoot in the direction your mouse is facing but NOT at a specific point. It is always at a specific elevation.
The issue I am having is when the player is moving the gun will shoot in random directions. Another issue I am having is any NPC’s that get hit by the bullet get damaged. And lastly, I don’t know a way to make the bullets stay at a perfect level.
-The goal of the shotgun is to shoot three wprojectiles ina stright line-
robloxapp-20221127-0854140.wmv (1.1 MB)
Code for Shotgun Bullet
local ShootPart = script.Parent
local Speed = 200
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.Parent = script.Parent
BodyVelocity.Velocity = ShootPart.CFrame.LookVector * Speed
ShootPart.Touched:Connect(function(TouchingPart, hit)
ShootPart.CanTouch = false
local Parent = TouchingPart.Parent
local humanoid = Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health = humanoid.Health -50
end
ShootPart:Destroy()
end)
Code for Shotgun
local Creator
local Move = true
script.Parent.FireGun.OnServerEvent:Connect(function(player,mouse)
Creator = player
local Shell = script.Parent.Handle.Shell:Clone()
Shell.Parent = workspace
Shell.Transparency = 0
Shell.Handle:Destroy()
Shell.Script.Enabled = true
Shell.CanCollide = true
local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(1,0,1) * 1000 --you can change this to whatever number you see fit. I prefer this one
BV.Velocity = Shell.CFrame.lookVector * -20
BV.Parent = Shell
if script.Parent.AmountOfShots.Value < 5 then
local Bullet = script.Parent.Bullet:Clone()
Bullet.CanTouch = false
Bullet.Transparency = 0
Bullet.Parent = workspace
Bullet.Name = "Bullet"
Bullet.Handel:Destroy()
Bullet.Trail.Enabled = true
Bullet.Script.Enabled = true
local Bullet2 = script.Parent.Bullet2:Clone()
Bullet2.CanTouch = false
Bullet2.Transparency = 0
Bullet2.Parent = workspace
Bullet2.Name = "Bullet"
Bullet2.Handel:Destroy()
Bullet2.Trail.Enabled = true
Bullet2.Script.Enabled = true
local Bullet3 = script.Parent.Bullet3:Clone()
Bullet3.CanTouch = false
Bullet3.Transparency = 0
Bullet3.Parent = workspace
Bullet3.Name = "Bullet"
Bullet3.Handel:Destroy()
Bullet3.Trail.Enabled = true
Bullet3.Script.Enabled = true
delay(0.2, function()
Bullet.CanTouch = true
Bullet2.CanTouch = true
Bullet3.CanTouch = true
end)
end
local Front = script.Parent.Fire
--Front.ParticleEmitter.Enabled = true
--Front.Smoke.Enabled = true
wait(0.1)
--Front.ParticleEmitter.Enabled = false
--Front.Smoke.Enabled = false
end)
HELP!
Link to model = https://www.roblox.com/library/configure?id=11673346516#!/general
2 Likes
RefusalMan
(RefusalMan)
November 27, 2022, 2:01am
2
Did you try putting the bullets on Massless?
I just did but this does not solve the problem of it shooting in random directions while the player walks
1 Like
adam5805
(Linen)
November 27, 2022, 4:07am
4
xXxOni_LordxXx:
local Creator
local Move = true
script.Parent.FireGun.OnServerEvent:Connect(function(player,mouse)
Creator = player
local Shell = script.Parent.Handle.Shell:Clone()
Shell.Parent = workspace
Shell.Transparency = 0
Shell.Handle:Destroy()
Shell.Script.Enabled = true
Shell.CanCollide = true
local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(1,0,1) * 1000 --you can change this to whatever number you see fit. I prefer this one
BV.Velocity = Shell.CFrame.lookVector * -20
BV.Parent = Shell
if script.Parent.AmountOfShots.Value < 5 then
local Bullet = script.Parent.Bullet:Clone()
Bullet.CanTouch = false
Bullet.Transparency = 0
Bullet.Parent = workspace
Bullet.Name = "Bullet"
Bullet.Handel:Destroy()
Bullet.Trail.Enabled = true
Bullet.Script.Enabled = true
local Bullet2 = script.Parent.Bullet2:Clone()
Bullet2.CanTouch = false
Bullet2.Transparency = 0
Bullet2.Parent = workspace
Bullet2.Name = "Bullet"
Bullet2.Handel:Destroy()
Bullet2.Trail.Enabled = true
Bullet2.Script.Enabled = true
local Bullet3 = script.Parent.Bullet3:Clone()
Bullet3.CanTouch = false
Bullet3.Transparency = 0
Bullet3.Parent = workspace
Bullet3.Name = "Bullet"
Bullet3.Handel:Destroy()
Bullet3.Trail.Enabled = true
Bullet3.Script.Enabled = true
delay(0.2, function()
Bullet.CanTouch = true
Bullet2.CanTouch = true
Bullet3.CanTouch = true
end)
end
local Front = script.Parent.Fire
--Front.ParticleEmitter.Enabled = true
--Front.Smoke.Enabled = true
wait(0.1)
--Front.ParticleEmitter.Enabled = false
--Front.Smoke.Enabled = false
end)
Try this and let me know if it works
local Creator
local Move = true
script.Parent.FireGun.OnServerEvent:Connect(function(player,mouse)
Creator = player
local Shell = script.Parent.Handle.Shell:Clone()
Shell.Parent = workspace
Shell.Transparency = 0
Shell.Handle:Destroy()
Shell.Script.Enabled = true
Shell.CanCollide = true
local ElevatedY = 10 -- or Shell.Position.y --change to anything you want :D
local newPos = Shell.CFrame.LookVector
newPos = Vector3.new(newPos.X, ElevatedY, newPos.Z)
local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge) --you can change this to whatever number you see fit. I prefer this one
BV.Velocity = newPos * -20
BV.P = 10000
BV.Parent = Shell
if script.Parent.AmountOfShots.Value < 5 then
local Bullet = script.Parent.Bullet:Clone()
Bullet.CanTouch = false
Bullet.Transparency = 0
Bullet.Parent = workspace
Bullet.Name = "Bullet"
Bullet.Handel:Destroy()
Bullet.Trail.Enabled = true
Bullet.Script.Enabled = true
local Bullet2 = script.Parent.Bullet2:Clone()
Bullet2.CanTouch = false
Bullet2.Transparency = 0
Bullet2.Parent = workspace
Bullet2.Name = "Bullet"
Bullet2.Handel:Destroy()
Bullet2.Trail.Enabled = true
Bullet2.Script.Enabled = true
local Bullet3 = script.Parent.Bullet3:Clone()
Bullet3.CanTouch = false
Bullet3.Transparency = 0
Bullet3.Parent = workspace
Bullet3.Name = "Bullet"
Bullet3.Handel:Destroy()
Bullet3.Trail.Enabled = true
Bullet3.Script.Enabled = true
task.delay(0.2, function()
Bullet.CanTouch = true
Bullet2.CanTouch = true
Bullet3.CanTouch = true
end)
end
local Front = script.Parent.Fire
--Front.ParticleEmitter.Enabled = true
--Front.Smoke.Enabled = true
task.wait(0.1)
--Front.ParticleEmitter.Enabled = false
--Front.Smoke.Enabled = false
end)
You have solved the collision problem. I appreciate it a lot!
2 Likes
adam5805:
local ElevatedY = 10 -- or Shell.Position.y --change to anything you want :D
local newPos = Shell.CFrame.LookVector
newPos = Vector3.new(newPos.X, ElevatedY, newPos.Z)
local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge) --you can change this to whatever number you see fit. I prefer this one
BV.Velocity = newPos * -20
BV.P = 10000
BV.Parent = Shell
I should have been more descriptive but the Volocity you edited was the velocity for the empty shell that flys out, NOT the bullet/projectile Sorry
The velocity for the bullet is in the bullet itself
1 Like
adam5805
(Linen)
November 27, 2022, 4:32am
7
Are you able to link the velocity in the bullet?
1 Like
Do you mind elaborating for me?
1 Like
adam5805
(Linen)
November 27, 2022, 4:50am
9
the path of the bodyvelocity for the bullet/projectile
1 Like
Are you asking if am I able to apply body velocity to the bullet? Because Yes and that is what I have done
1 Like