Hello, I'm making a lightning bolt script, and I want the bolt to explode when it touches something. However, the touched event doesn't get detected when it hits an NPC.
Example: link
local bolts = {}
local function makeBolts(start, goal)
local lastPos = start
local step = 5
local off = 15
local range = 100
local distance = (start - goal).magnitude
if distance > range then distance = range end
for i = 0, distance, step do
local offset = Vector3.new(
math.random(-off, off),
math.random(-off, off),
math.random(-off, off)
) / 10
local start = lastPos
local goal = start + (goal - start).unit * step + offset
local bolt = lightningBolt:Clone()
bolt.Parent = game.Workspace
bolt.CFrame = CFrame.new(start:Lerp(goal, 0.5), goal)
bolt.Size = Vector3.new(bolt.Size.X, bolt.Size.Y, (start - goal).magnitude)
table.insert(bolts, bolt)
lastPos = goal
game:GetService("Debris"):AddItem(bolt, 0.5)
end
for _, v in pairs(bolts) do
v.Touched:Connect(function(hit)
if not debounce and hit.Name ~= "Dawn's Birth" and hit.Name ~= "Smoke" then
debounce = true
script.Shake:FireClient(player)
lightningExplosion:Play()
module.CreateExplosion(magicType, bolts[#bolts].CFrame, bolts[#bolts])
for i in pairs(bolts) do
bolts[i] = nil
end
wait(0.5)
debounce = false
end
end)
end
end
local start = lightningBolt.Position
local goal = mouse
makeBolts(start, goal)
