# How to Make Lightning

How do I script Lightning I saw some old posts on it but they never gave a really good script on how to do it, only the logic. I’m not very good at scripting so Idk how to do it. Can someone help?

1 Like

You could use math.random to randomize the position. Then use Raycasting to create the part.

2 Likes

You can view this tutorial, and modify the script as you wish.
I don’t know if that, or something like that.

4 Likes

That won does not give the script RIP

How would I do that? I’m trying to make it “jump” between 2 parts

Wdym? It should give you the script its a tutorial, do you mean to copy and paste?

2 Likes

Yeah because I’m a slow typer welp

do you know how to make this script go from one part to another instead of just vertical?

``````local serv = game.ReplicatedStorage

function CastLightningDown(head) -- vector3 start position

local maxRange = 5 -- how far in polar coordinates can the lightning go
local heightOffset = 5 -- how far the 3D vector is offset

local boltParts = {}

while head.Y > 0 do -- the advantage of using a Vector3 as the end of the lightning bolt
local angle = math.pi*4*math.random() -- 4 because I don't trust the random distribution

local dir = Vector3.new(math.cos(angle)*radius,-heightOffset,-math.sin(angle)*radius) -- converting the XY plane of a graph into the XZ plane of a part requires Z to be negative

local newPart = serv.Lightning:Clone()
newPart.Size = Vector3.new(.1,.1,math.random(1,5)) -- component change to take advantage of CFrame's 2-vector construction method
newPart.CFrame = CFrame.new(head,head+dir)*CFrame.new(0,0,-newPart.Size.Z/2) -- starting point, target point, and offset by half the length
table.insert(boltParts,newPart)
head = newPart.CFrame*Vector3.new(0,0,-newPart.Size.Z/2) -- move the head to the end of the part so we can continuously cast bolts
newPart.Parent = workspace
end

end
while true do
for i = 1, 5 do -- make 30 lightning bolts
CastLightningDown(Vector3.new(0,100,0))
end
wait(.1)
local children = workspace:GetChildren()
for i, child in ipairs(children) do
if child.Name == "Lightning" then
child:Destroy()
end
end
end
``````
1 Like

change each part of the script, and see what they do

1 Like

I said you could use Raycasting to send an invisible ray to that part, then create a part out of it.

Here’s a developer page for Raycasting if you need it.

1 Like