How to make lightning using script

Hi Everyone!, this is tutorial for my previous system of generating lightning bolts or arc beams.

  1. Make two parts, finish and start. It have to be anchored !
  2. Insert script inside a game where you script your parts, you can do this for guns and tools where the finish is mouse hit position and start is tool handle

SCRIPTING:

  1. we need to create a variables
local model = workspace.Model ---- insert your model here
local number  = 5 ----- number of points are you creating
local index = 0 ---- an index that are gaps beetween each point
local start = workspace.Part1 --- replace it with your start part
local finish = workspace.Part2  --- replace with finish part

Okay! we created all we need to create a loops

for i = 1, number do 
local part = Instance.new("Part",script.Folder) --- you can change parent

part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(1,1,1)
part.Transparency = 0 --------- you can change it
part.BrickColor = BrickColor.new("New Yeller") --- customizable
part.Material = "Neon" --- also you can change it



end

When we maked a part, next step is add a position to this part
3.

start.CFrame = CFrame.lookAt(start.Position,Finish.Position)
part.CFrame = start.CFrame
part.Position = part.Position + start.CFrame.LookVector * index
part.CFrame = part.CFrame * CFrame.new(math.random(-1,1),math.random(-1,1),0) --- don't change it, the look vector is Z axis and it can break the script
index = index + (start.Position - finish.Position).Magnitude/number 

if (part.Position - start.Position).Magnitude < ((start.Position - finish.Position).Magnitude/number) then
	part:Destroy()
end
wait(0.1)

now we have point generation
4.

wait(0.1)
for _, v in pairs(script.Folder:GetChildren()) do
	if v and v:IsA("BasePart") and (script.Folder:GetChildren()[_-1]) ~= nil then
		v.CFrame = CFrame.lookAt(v.Position,script.Folder:GetChildren()[_-1].Position)
		local midpoint = (script.Folder:GetChildren()[_-1].Position + v.Position)/2
		
		local bolt = Instance.new("Part",script.Folder)
		bolt.Anchored = true
		bolt.CanCollide = false
		bolt.Size = Vector3.new(0,0,(script.Folder:GetChildren()[_-1].Position - v.Position).Magnitude) --- experiment with magnitude, try paste it to axis
		bolt.Position = midpoint
		bolt.CFrame = CFrame.lookAt(v.Position)
		
		--------------- paste here another variables like material or color
		
	elseif (script.Folder:GetChildren()[_-1]) == nil then
		v.CFrame = CFrame.lookAt(v.Position,start.Position)
	wait(0.1) -- you can delete it, but is not recommended
	end
end

There we go, if you wan’t. you can add more things like, corountines or make more effects, but is basics of this sytem. sorry for my english and i’m think i help you. Bye

5 Likes

From a simple look, I cant tell if the lightning would look good, but I will take your word for it

How would I make it hurt people

1 Like

use for loop to get the parts from the folder, i tested this lightning and it work like on this I'm made a lightning generator part - this is the same script but in show version

In the video it’s looking great but when I do it myself it won’t work. I specify 5 points and it makes 15 points. All these points aren’t alligned properly. Also the bolt never exists. It never makes it. There is also a few issues in your script like using “_” the script does not recognize this and errors which is probably the reason it’s not working. Also you should consider making “script.Folder” a variable since it’s used alot in the script and it’s easier to change 1 value than every value in the script. Also you used CFrame.lookAt() in your script but only gave 1 argument instead of 2. Consider testing your script before releasing.


I specified 5 points, it made 15 points but only 2 points are visible and the bolt is not created.

Oh, sorry, i can give you my script inside part, to help you, here is script:

script.Parent.ClickDetector.MouseClick:Connect(function()
	local Folder = workspace.Lfolder
	local number = 20
	local index = 0
	local nextPart = nil
	local previousPart = nil
	for i = 1, number do
		index = index + 1
	
		local part = Instance.new("Part",Folder)
		part.Anchored = true
		part.CanCollide = false
		part.Size = Vector3.new(1,1,1)
		part.CFrame = script.Parent.CFrame * CFrame.new(math.random(-3,3),index*5,math.random(-3,3))
		part.Material = "Neon"
		part.Name = "Part"..i
		part.BrickColor = BrickColor.new("White")
		
		if i == number then
			for _ , v in pairs(Folder:GetChildren()) do
				if v:IsA("BasePart")and(Folder:GetChildren()[_-1])~= nil then
					nextPart = Folder:GetChildren()[_+1]
					previousPart = Folder:GetChildren()[_-1]
					local Midpoint = (v.Position + previousPart.Position)/2
					
					local newPart = Instance.new("Part",workspace.Pfolder)
					newPart.Anchored = true
					newPart.Position = Midpoint
				
					newPart.Size = Vector3.new(0.5,0.5,(v.Position - previousPart.Position).Magnitude)
					newPart.Material = "Neon"
					newPart.BrickColor = BrickColor.new("Baby blue")
				newPart.CFrame = CFrame.lookAt(v.Position,previousPart.Position)
					v.CFrame = CFrame.lookAt(v.Position,previousPart.Position)
					
					newPart.Transparency = 0.25
					newPart.CFrame = newPart.CFrame * CFrame.new(0,0,(v.Position - previousPart.Position).Magnitude/-2)
					newPart.CastShadow = false
						
		
		elseif (Folder:GetChildren()[_-1])== nil then
					v.CFrame = CFrame.lookAt(v.Position,script.Parent.Position)
					previousPart = script.Parent
					local Midpoint = (v.Position + previousPart.Position)/2

					local newPart = Instance.new("Part",workspace.Pfolder)
					newPart.Anchored = true
					newPart.Position = Midpoint

					newPart.Size = Vector3.new(0.5,0.5,(v.Position - previousPart.Position).Magnitude)
					newPart.Material = "Neon"
					newPart.BrickColor = BrickColor.new("Baby blue")
					newPart.CFrame = CFrame.lookAt(v.Position,previousPart.Position)
					v.CFrame = CFrame.lookAt(v.Position,previousPart.Position)
					newPart.CastShadow = false
					newPart.Transparency = 0.25
					newPart.CFrame = newPart.CFrame * CFrame.new(0,0,(v.Position - previousPart.Position).Magnitude/-2)
				
				end
				wait(0.1)
			
			end
			task.wait(0.1)
			previousPart = nil
			nextPart = nil
			index = 0
			for int, p in pairs(workspace.Lfolder:GetChildren()) do
				if p and p:IsA("BasePart") then
					p:Destroy()
					
					if #workspace.Lfolder:GetChildren()== 0 then
						break
						
					end
					wait(0.1)
				
				end
			end
			task.wait(0.1)
		
			break
		end
		wait(0.1)
	end
end)
```, ps i tested it with two folders, that will maybe help you
1 Like

Thank you! I will check it out later just wanted to let you know.

1 Like

no problem, i’m know how hard is make this, if you don’t know how to start

Make it detect when a part in the partfolder .hits a player it deals damage.

I want to let you know that this is really awesome! It’s satisfying to look at. I’m just trying to make it have a start and end location like in your tutorial but I will figure that out. I also made the lightning dissapear instead of permanently staying in the game!

https://i.gyazo.com/95e438aee65757d8f4448e988572b168.mp4

1 Like

Nice, when i post this tutorial it’s my first try, now i figure how to do with finish and end.

1 Like