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

Lightning function giving an error?

I wrote a function that generates lightning using lookVector, etc, and when I call the function I get the following error

Workspace.Player.Right Leg.Wall2:9: bad argument #1 to 'new' (Vector3 expected, got nil)

(Line 9 in the error is line 5 in the below code) I have no idea how to rectify this. 'Wall' Is the LocalScript in which this function is contained. Here is the function just in case it is needed:

function Lightning(pos,pos2,radius,numParts,model)
	radius = 0.2
	numParts = 5
	model = game.Players.LocalPlayer.Character
	local lv = CFrame.new(pos,pos2).lookVector
	local dist = (pos-pos2).magnitude
	local dbp = dist/numParts
	local last = pos
	for i = 1,numParts do
		local p = Instance.new("Part",model)
		p.FormFactor = "Symmetric"
		p.Size = Vector3.new(1,1,1)
		p.CanCollide = false
		p.Anchored = true
		p.BrickColor = BrickColor.new("Bright red")
		local x = math.random(-100,100)/100*dbp/2
		local y = math.random(-100,100)/100*dbp/2
		local p2 = CFrame.new(pos+lv*(i*dbp),pos2+lv)*CFrame.new(x,y,0)
		local dist2 = (p2.p-last).magnitude
		local mid = (p2.p+last)/2
		local m = Instance.new("BlockMesh",p)
		m.Scale = Vector3.new(radius,radius,dist2)
		p.CFrame = CFrame.new(mid,p2.p)
		last = p2.p
		game:GetService("Debris"):AddItem(p,math.random(40,100)/1000)
	end
end

Help would be appreciated. Thanks.

6 comments

0

The error isn't in the lightning function.

0

That is where you are wrong, the error specifically tells me Line 9 (line 5 here)

1

What arguments are you giving for pos1 and pos2?

0

There are more efficient ways of constructing a unit vector between two points...

0

Well I'm not very good at scripting and I only made this with help.

0

How are you calling the Lightning function?

1 answer

You aren't calling Lightning() properly

The function expects a Vector3 for arguments #1 and #2. By the looks of things, you are only calling it as Lightning() which is incorrect as it requires arguments within the parentheses in order to satisfy the requirements in the parameters.

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