Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Lightning bolt only explodes when in contact with humanoid?

I've made a lightning bolt via some tutorials, and I want it to explode when it hits something.

-- script
local function makeBolt(start, goal)
	local lastPos = start
	local step = 10
	local off = 20
	local distance = (start - goal).magnitude
	local range = 110
	
	if distance > range then distance = range end
	
	for i = 0, distance, step do
		local start = lastPos
		local offset = Vector3.new(
			math.random(-off, off),
			math.random(-off, off),
			math.random(-off, off)
		) / 10
		local goal = start + (goal - start).unit * step + offset
		local bolt = lightningBolt:Clone()
		bolt.Parent = game.Workspace
		bolt.Name = "LightningBolt"
		bolt.Size = Vector3.new(bolt.Size.X, bolt.Size.Y, (start - goal).magnitude)
		bolt.CFrame = CFrame.new(start:Lerp(goal, 0.5), goal)
		
		for i, v in pairs(workspace:GetChildren()) do
			if v.Name == "LightningBolt" then
				v.Touched:Connect(function(hit)
					if hit.Name ~= "Dawn's Birth" or "Handle" then
						local explosion = game.ServerStorage:WaitForChild("Misc").Explosion6:Clone()			
						explosion.CFrame = v.CFrame
						explosion.Parent = game.Workspace
						lightningExplosion:Play()
						
						v:Destroy()
						
						local co1 = coroutine.create(function()
							if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= player.Name then
								local part = Instance.new("Part")
								part.Anchored = true
								part.Transparency = 1
								part.CanCollide = false
								part.Position = hit.Position
								part.Parent = game.Workspace
								
								local damageIndicatorGUI = game.ServerStorage.Misc.DamageIndicator:Clone()
								damageIndicatorGUI.Parent = part
								damageIndicatorGUI.Indicator.Text = damage
								
								hit.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
								
								wait(0.5)
								
								for i = 0, 1, 0.1 do
									damageIndicatorGUI.Indicator.TextTransparency = i
									damageIndicatorGUI.Indicator.TextStrokeTransparency = i
									wait(0.05)
								end
								
								part:Destroy()
							end
						end)
						
						coroutine.resume(co1)
						
						for i = 2, 10, 0.2 do
							explosion.Size = Vector3.new(i, 2, i)
							game:GetService("RunService").Heartbeat:Wait() 
						end
						
						explosion:Destroy()
					end					
				end)
			end
		end
		
		game:GetService("Debris"):AddItem(bolt, 0.5)
		lastPos = goal
	end
end

local start = lightningBolt.CFrame.p
local goal = mouse.p
makeBolt(start, goal)

1 answer

ok when your lightning bolt hits a player at line 37 right after that insert an explosion on that part.

2 comments

0

the explosion should appear when something gets touched, but the explosion only appears when a humanoid touches it.

0

OOOhhhh

This discussion is preserved as it was. New answers, comments, and votes are closed.