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

Attempt to index local 'player'? I'm not sure to define the player in a regular function?

The error is on Line 16. "Attempt to index local 'player' (a nil value)" How would I define the player from a regular function. I tried putting the player in as a parameter but it didn't work.

Script in ServerScriptService Error on Line 16

leds = {}

function fade()
	for i, v in pairs(leds) do
		if v~= nil then
			v.Transparency = v.Transparency + 0.2
			if v.Transparency > 1 then
				v:Destroy()
			end
		end
	end
end


function makelig(player)
	local b = player.Character:GetChildren()
	for i = 1, #b do
	if b[i].Name == "Left Arm" or b[i].Name == "Left Leg" or b[i].Name == "Right Arm" or b[i].Name == "Right Leg" then	
	local tip = b[i]
	local lastpoint = b[i].Position
	local tool = b[i]
	if tip~= nil then
		local off = tip.Size.z/2
		local point = (tip.CFrame*CFrame.new(0, 0, -off)).p
		local unit = (lastpoint - point).unit
		local mag = (lastpoint - point).magnitude
		if tool.Parent.Humanoid.WalkSpeed > 55 and mag>10 then
			local middle = lastpoint-(unit*(mag/2))
			local cf = CFrame.new(middle, point)
			local l = Instance.new("Part")
			l.Shape = tip.Shape
			l.Name = "lightning"
			l.Anchored = true
			l.CanCollide = false
			l.Size = Vector3.new(0.2, 0.2, mag)
			l.BrickColor = BrickColor.new("Curry")
			l.Reflectance = tip.Reflectance
			l.Material = "Neon"
			l.TopSurface = 0
			l.Transparency = 0
			l.BottomSurface = 0
			l.Parent = tip
			l.CFrame = cf
			table.insert(leds, l)
			lastpoint = point
		end
		end
		end
	end
end





game.ReplicatedStorage.Lightning.OnServerEvent:Connect(function(player)
	game:GetService("StarterGui").ControlHUD.Frame.Plus.Lightning.Control:Clone().Parent = player.PlayerGui
	--player.PlayerGui:WaitForChild("ControlHUD").Frame.Plus.Lightning.Control:Clone().Parent = player.PlayerGui
		while true do
		makelig()
		wait()
		fade()
		wait()
	end
end)

LocalScript in TextButton

script.Parent.MouseButton1Down:Connect(function()
	game.ReplicatedStorage.Lightning:FireServer()
end)

1 comment

0

On line 60 of the script you did not give it a player, makelig(player)

1 answer

✓ Accepted answer

#Parameters and Arguments

I don't think you understand how parameters and arguments for functions work.

When you define a function with parameters(things inside the parenthasis) then you're making the function expect some kind of input.

Take this script for example:

function a(b)
	print(b + 3)
end

a(2) --> 5

Notice the function was defined with the 'b' parameter - and that the parameter was then used inside the code.

'b' is a keyword to hold the first argument of input given by the function call. Notice on line 5 how the function was called with a '2'. In this situation, '2' is what 'b' is predefining.


Application

So, now lets apply this to your code. On line 15 you defined the 'makelig' function with a 'player' parameter. Then, on line 60 you called the function without suppying a 'player' argument. You need to call the function WITH arguments that correspond to the parameters you defined the function with.

makelig(player)

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