I have a hostile Shotgun NPC and it works perfectly fine except for the bullets. Since this NPC’s weapon is a shotgun, I would want it to shoot multiple bullets at once and once those bullets touch something that has a humanoid it would damage them, but if it’s a part and not a humanoid, it would create debris on the impact point to make it seem more realistic. Well, this works when there is only 1 bullet but when there are multiple, it doesn’t seem to do what I intend it to do. It seems like when there are multiple bullets, it stops working because the bullets touched one another already, I tried making the script also check to make sure that what the bullet is touching isn’t another bullet.
Here are some videos to demonstrate:
One Bullet(Works Perfectly Fine):
One Bullet Collision(Also Fine):
Multiple Bullets(This is the problem):
Multiple Bullets Collision(Also The problem):
So basically what I want to achieve here is that I want the same results for the one bullet but for multiple bullets. I don’t seem to understand why when there are multiple bullets, it’s not working as I intend it to.
Here is the script where the bullets are being created:
function shoot(target)
local flash = npc.Shotgun.Flashpointer.Flash
local shotgun = npc.Shotgun.Handle
local bulletspeed = 150
local bulletDuration = 3
local bullet
lookatTarget(target)
for i = 1, 6, 1 do
bullet = Instance.new("Part")
local bv = Instance.new("BodyVelocity")
local pointlight = Instance.new("PointLight")
pointlight.Range = 4
pointlight.Brightness = 1
pointlight.Color = Color3.new(1, 1, 0)
pointlight.Shadows = true
bullet.Anchored = false
bullet.Color = Color3.new(1, 1, 0)
bullet.Velocity = shotgun.CFrame.LookVector * bulletspeed
bullet.Material = Enum.Material.Neon
bullet.Size = Vector3.new(0.165, 0.156, 0.549)
bullet.Position = shotgun.Position
bullet.CFrame = shotgun.CFrame
bullet.Name = "bullet"
bv.P = math.huge
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Velocity = bullet.Velocity
pointlight.Parent = bullet
bv.Parent = bullet
bullet.Parent = workspace
bullet:SetNetworkOwner(nil)
debris:AddItem(bullet, bulletDuration)
end
flash:Emit(1)
bullet.Touched:connect(function(hit)
local myFaction = npc.Faction
local faction = hit.Parent:FindFirstChild("Faction")
local human = hit.Parent:FindFirstChild("Humanoid")
local hrp = hit.Parent:FindFirstChild("HumanoidRootPart")
local part = hit:IsA("Part")
if faction then
if human and hrp and human.Health > 0 and hit.Parent ~= npc and faction.Value ~= myFaction.Value then
local bulletdmg = math.random(5,7)
human:TakeDamage(bulletdmg)
bullet:Destroy()
end
else if part and not human and not hrp and not faction and hit ~= shotgun and hit ~= npc.Shotgun.Flashpointer and hit ~= npc.Beard.Handle and hit.Name ~= "bullet" then
local dmgPart = Instance.new("Part")
dmgPart.Size = Vector3.new(.5,.5,.1)
dmgPart.Color = hit.Color
dmgPart.Material = hit.Material
dmgPart.Velocity = Vector3.new(math.random(-10,10),math.random(-10,10),math.random(-10,10))
dmgPart.Position = bullet.Position
dmgPart.CFrame = bullet.CFrame
dmgPart.Parent = workspace
bullet:Destroy()
ObjectImpact.objectimpact(dmgPart) -- Just random impact sounds
debris:AddItem(dmgPart, 2)
end
end
end)
end
Also how would I go on making spread for part velocity with bodyvelocity bullets? Solutions I’ve seen are only for raycasting which I don’t really seem to know how to convert it for bodyvelocity.
I could do this:
local spread = math.random(-0.5,0.5)
bullet.Velocity = (shotgun.CFrame.LookVector + Vector3.new(spread,spread,spread)) * bulletspeed
bv.Velocity = bullet.Velocity
Note: The body velocity is set to the bullet velocity.
This way doesn’t seem to be good so is there another way?