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

How does this function keep passing a nil value?

I am currently using this program and here it is.

function run(i)
	print(i)	
end
script.Parent.Place1.MouseButton1Click:Connect(run(1))
script.Parent.Place2.MouseButton1Click:Connect(run(2))
script.Parent.Place3.MouseButton1Click:Connect(run(3))

When I run this program it keeps saying Attempt to call a nil value. Please tell me why this isn't working!

1 comment

0

maybe you should say : print(run)

1 answer

✓ Accepted answer

What is happening when this code is ran is that Connect() tries to run the function inside the parenthesis.

run by itself is a function run(3) is calling the function run.

When you say Connect(run) you are running the function when you click the mouse. When you say Connect(run(3)) you are calling run, which returns nil so the what the code actually sees Connect(nil) resulting in your error.

You should instead do the following:

function run(i)
    print(i)    
end

script.Parent.Place1.MouseButton1Click:Connect(function() run(1) end)
script.Parent.Place2.MouseButton1Click:Connect(function() run(2) end)
script.Parent.Place3.MouseButton1Click:Connect(function() run(3) end)

2 comments

0

:D

0

Thanks this is really really helpful

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