Lightning beam effect

how would i acomplish something similar to this


i know it is made with parts and not a beam but that is about it

To accomplish this, you’d need to:

  1. You will need to put all the instances (Cylinders) inside a table so create a table
  2. Create the cylinders with a loop and u insert the each cylinder in the table. Example:
  for i = 1, 15 do -- the 15 depends on the num of the cylinders that u want
	local cylinder = Instance.new("Part")
 	cylinder.Shape = Enum.PartType.Cylinder
 	cylinder.Size = Vector3.new(1, 1, 1)  
 	cylinder.Anchored = true
 	cylinder.CanCollide = false
 	cylinder.Material = Enum.Material.Neon  
 	cylinder.BrickColor = BrickColor.new("Bright red")  
	cylinder.Transparency = 0.1  
 	cylinder.Parent = workspace
	table.insert(cylinders, cylinder)
end
  1. U create a function for the Lightning so u have to get the start and the end pos of where u want them to land (i will use a while true just to show so it will have the coordinates of the lookvector of a part). In this function u have to get the unit of the 2 positions so u get the direction, then u get the distance, and u calculate the segment lenght that will be Distance/Cylinder Count (the 15 cylinders of before).
    The actual function its again inside a loop that goes from 1 to the cylinder count and inside this loop lot of stuff gonna happen: You gonna set a random direction cuz u dont want them to be plain, but u want em to look like a lightning so u gonna do the direction + the unit of a randomized vector3. Now You calculate the next position by doing CurrentPos (the Starting Position) + the random direction * the segment length. Then u get the distance that between the next position and the current one. And then here the last stuff cuz: You set the size and the cframe by doing

cylinder.Size = Vector3.new(actualDistance, math.random(50,100)/100, math.random(50,100)/100)
cylinder.CFrame = CFrame.new(currentPos:Lerp(nextPos, 0.5), nextPos) * CFrame.Angles(math.rad(90), 0, math.rad(90))

after this u set the CurrentPosition to be the next one so it goes on and (i added it cuz i think there is in the vid cuz it looks like it) u add a dissolve of the cylinder, so the further it is higher is the transparency.
by doing:

cylinder.Transparency = 0.1 + (i - 1) * 0.05

As i said i made all of this for a test so its with a while true, but u can change that freely with a tool or whatever.

Imma give you all of the script i made for you so you can modify it and understand better what i’m saying.

local cylinderCount = 15
local cylinders = {}

for i = 1, cylinderCount do
	local cylinder = Instance.new("Part")
	cylinder.Shape = Enum.PartType.Cylinder
	cylinder.Size = Vector3.new(1, 1, 1)  
	cylinder.Anchored = true
	cylinder.CanCollide = false
	cylinder.Material = Enum.Material.Neon  
	cylinder.BrickColor = BrickColor.new("Bright red")  
	cylinder.Transparency = 0.1  
	cylinder.Parent = workspace
	table.insert(cylinders, cylinder)
end

local function createLightning(startPos, endPos)
	local direction = (endPos - startPos).unit
	local distance = (endPos - startPos).magnitude
	local segmentLength = distance / cylinderCount

	local currentPos = startPos

	for i = 1, cylinderCount do
		local cylinder = cylinders[i]

		local randomDirection = direction + Vector3.new(math.random(-10, 10) * 0.01, math.random(-10, 10) * 0.01, math.random(-10, 10) * 0.01).unit
		local nextPos = currentPos + randomDirection * segmentLength

		local actualDistance = (nextPos - currentPos).magnitude
		
		cylinder.Size = Vector3.new(actualDistance, math.random(50,100)/100, math.random(50,100)/100)  
		cylinder.CFrame = CFrame.new(currentPos:Lerp(nextPos, 0.5), nextPos) * CFrame.Angles(math.rad(90), 0, math.rad(90))

		currentPos = nextPos

		cylinder.Transparency = 0.1 + (i - 1) * 0.05
	end
end

local Part = script.Parent 

while true do
	local startPos = Part.Position
	local endPos = startPos + Part.CFrame.lookVector * math.random(40, 50) -- i randomized the end position cuz i thought it would have looked better  
	createLightning(startPos, endPos)
	wait(0.05)  
end

I hope i explained it rightly, and i hope u understood.

1 Like