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

When I put this code in it say "disconnected", and "attepted a nil value". Did I do something wrong?

Asked by 10 years ago
local brick = Workspace.InvisibleBrick

brick.Touched:connect(onTouch)
function onTouch(Part)
brick.Transparency = 1 
wait(1) 
brick.Transparency = 0


end

2 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Scripts execute top to bottom.

So, when we get to line 3, it looks for onTouch, but there isn't anything called onTouch, since it hasn't been made yet. Variables that have not yet been defined are nil, which is where that error comes from.

Move the connection line below the function definition.

Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

You're trying to connect an event to a nil value. On line 03, the onTouch function does not exist. It only exists after line 04, because that's when you create the function. This can be fixed easily by placing the connection line after the function is created.

function onTouch(Part)
    --stuff
end
brick.Touched:connect(onTouch)

Answer this question