Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How does this function keep passing a nil value?

Asked by 5 years ago

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!

0
maybe you should say : print(run) TheRealPotatoChips 793 — 5y

1 answer

Log in to vote
5
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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)
0
:D greatneil80 2647 — 5y
0
Thanks this is really really helpful superbolt999 36 — 5y
Ad

Answer this question