Lightning bolts don't do anything, even after inserting scripts that change their CFrame? How do I fix this?

  1. What do you want to achieve? A lightning module with no spoonfed code (I’m just asking for help, OK?)

  2. What is the issue? My lightning bolts do ABSOLUTELY NOTHING, I insert a script that supposedly makes them move but they don’t.

  3. What solutions have you tried so far? Brought my friend to help, checked what the script’s parent is (I parent the script to the part but when it isn’t it’s disabled, can’t be too careful though), checked for typo errors, no errors or warnings or anything. I have been procrastinating for hours though, and that’s because I got no solutions or anything for a while.

Here’s the code I’m inserting in the bolts:

local name = script.Parent.Name --Gets the name of the script's parent
print(name)
local str = string --Gets the string library
local split = str.split(name, " ") --splits the parent's name
local toLookAt = split[3] --Gets the 3rd item from the split name
local runService = game:GetService("RunService") --gets RunService
local currentAssignedName = str.split(script.Parent.Parent.Name, " ")
runService.Heartbeat:Connect(function()
	script.Parent.CFrame = CFrame.lookAt(script.Parent.Position, ((script.Parent.Parent.Parent:WaitForChild("LightningPoints " .. currentAssignedName[2])):WaitForChild(toLookAt)).Position)
	task.wait()
end)

And just in case, here’s the module (Everything works here, I just commented every line for convenience:

local lightningmodule = {} --Lightning module
local assignLightningName = "1" --Assigned number for order
local random = Random.new() --Gets a Random seed
local defaultColor = Color3.fromRGB(0, 127, 255) --Default lightning color
local movePointScript = script:WaitForChild("MoveScript") --Move Script for the points
local boltScript = script:WaitForChild("BoltScript") --Zigzagging script for the bolts
local errorMessages = { --Table of error messages
	"Expected %s, got %s" --Expected ___, got ___
} --Closes the table of error messages
local function lightningBoltCreate(pointFolder : Instance) --Function that creates the lightning bolts
	local boltFolder = Instance.new("Folder") --Creates a folder for the bolts
	boltFolder.Name = "BoltFolder " .. assignLightningName --Names the folder
	boltFolder.Parent = game.Workspace --Parents it
	for index, child in ipairs(pointFolder:GetChildren()) do --Loops through every child in the folder of points
		if index == 1 then continue end --Skips if it has an index of 1
		local a1stPoint = ((pointFolder:GetChildren())[index-1]) --Gets the point before it (point with an index of the current index -1)
		local bolt = Instance.new("Part") --Creates a bolt
		bolt.Color = Color3.fromRGB(0, 255, 255) --Colors the bolt (Will be replaced, current color for testing)
		bolt.Material = Enum.Material.Neon --Bolt's material turns to neon
		bolt.Size = Vector3.new(1, 1, (a1stPoint.Position - child.Position).Magnitude) --Changes the size of the bolt to fit between 2 points
		bolt.CFrame = a1stPoint.CFrame:Lerp(child.CFrame, 0.5) --Sets the bolt's CFrame to right between the 2 points
		bolt.CFrame = CFrame.lookAt(bolt.Position, child.Position) --Sets the CFrame to look at the point in front of it
		bolt.Name = "Bolt " .. a1stPoint.Name .. " " .. child.Name --Names the bolt
		bolt.Anchored = true --Anchors the bolt
		bolt.Parent = boltFolder --Parents the bolt to the Bolt Folder
		local boltLight = Instance.new("PointLight") --Creates a point light for the bolt to emit light
		boltLight.Color = bolt.Color --Sets the emitted light to the bolt's light
		boltLight.Parent = bolt --Parents the light to the bolt
		local scriptClone = boltScript:Clone() --Creates a script that makes it always look at the point in front of it every frame
		scriptClone.Parent = bolt --Parents it to the bolt
		scriptClone.Disabled = false --Enables the script
	end --Closes the for loop
end --Closes the function
local function lightningPointCreate(amountOfPoints : number, cframes : table, color : Color3, isStraight : boolean, parent : Instance) --Function for creating the points
	for i = 1, math.floor(amountOfPoints) do --Loops through the amount of points
		local currentTable = cframes[i] --Gets the table in the table of CFrames with index of i
		local currentCFrame = currentTable[1] --Gets the actual CFrame in the CFrame table
		local point = Instance.new("Part") --Creates a point
		point.Size = Vector3.new(1, 1, 1) --Resizes the point to a very small block
		point.Anchored = true --Anchors the point
		point.CFrame = currentCFrame --Sets the CFrame to the currentCFrame (defined earlier above)
		point.Color = color --Sets the point's color to the given color
		point.Transparency = 0.75 --Sets the point's transparency to 0.75
		point.CanCollide = false --Set point's CanCollide to false
		point.Parent = parent --Sets the point's parent to the given parent
		point.Name = "Point" .. i --Names the point
		if isStraight == false then --If isStraight boolean is false,
			local scrip = movePointScript:Clone() --Clones a move script, which moves the points up & down
			scrip.Disabled = false --Script enabled
			scrip.Parent = point --Parents the script to the point so it can move
		end --Closes the if statement
	end --Closes the for loop
end --Closes the function
function lightningmodule.Create(point1 : Instance, point2 : Instance, color : Color3, isStraightLine : boolean) --Function to create the actual lightning, above functions are just for creating the points and bolts, actual handler function
	assert(typeof(isStraightLine) == "boolean" or typeof(isStraightLine) == "nil", string.format(errorMessages[1], "boolean", typeof(isStraightLine))) --Makes sure isStraightLine is either a boolean or nil (nil is by default true)
	if color == nil then --If the given color isn't given or nil
		color = defaultColor --color will be set to default color instead
	end --Ends the above if statement
	if isStraightLine == nil then --If isStraightLine is nil
		isStraightLine = true --isStraightLine will be set to true by default
	end --Ends the above if statement
	point2.Orientation = point1.Orientation --Sets point 2 (end point)'s orientation to point 1 (starting point)'s orientation
	local timesToGetLightningStrike = math.random(5, 6) --Gets the amount of points
	local tableOfLerps = {} --Creates a table for storing both CFrames and lerp alpha
	for i = 1, timesToGetLightningStrike do --Loops from 1 to timesToGetLightningStrike
			local lerpAlpha = i/timesToGetLightningStrike - random:NextNumber(0.05, 0.1) --Gets a random lerp alpha
			print(lerpAlpha, lerpAlpha < 1 and lerpAlpha > 0) --Prints the lerp and whether it meets the given conditions
			if lerpAlpha >= 1 or lerpAlpha <= 0 then --If it doesn't meet them
				repeat --Repeats
					print(lerpAlpha, lerpAlpha < 1 and lerpAlpha > 0)
					task.wait()
				until lerpAlpha < 1 and lerpAlpha > 0 --Until it fulfills the 2 conditions
			end --closes the if statement above
			local lerpCFrame = point1.CFrame:Lerp(point2.CFrame, lerpAlpha) --Creates a lerp for a created point
			local lastTable = {lerpCFrame, lerpAlpha} --Creates a table containing the lerp and the CFrame
			table.insert(tableOfLerps, lastTable) --Inserts the table above in tableOfLerps
	end --Closes the for loop
	local folderOfPoints = Instance.new("Folder") --Creates a folder for the points
	folderOfPoints.Name = "LightningPoints " .. assignLightningName --Names it
	folderOfPoints.Parent = game.Workspace --Parents the folder to workspace
	lightningPointCreate(timesToGetLightningStrike, tableOfLerps, color, isStraightLine, folderOfPoints, point1) --Creates the points
	lightningBoltCreate(folderOfPoints) --Creates the bolts
	assignLightningName = tostring(tonumber(assignLightningName)+1) --Adds to the assignLightningName
end --Closes the function
return lightningmodule --Returns the module

If you need anything else, feel free to ask.

Use bodymovers/bodyforces to make them move.

I’m not experienced with these type of things, but I’ll try.

EDIT: I just want them to change orientation, and I also want the opportunity to instantly make it change orientation (Doing this by CFrame.lookAt), how would I achieve this @Limited_Unique?

you don’t need a task.wait inside a heartbeat connection

1 Like

Alright, I’ll update this as soon as I can, I’m on my study laptop at the moment because I have an exam next week.
EDIT: So I fixed everything there, and it turns out that it works IF the points are far enough, but why does this happen?