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

What does the variable mean in this function? [Solved]

I have a LocalScript in a ClickDetector which is in a part. In the LocalScript I have a function;

script.Parent.MouseClick:Connect(function()

end

If i understand correct, the word inside the brackets after function is a variable. I don't have a word there now but if I would have, what does it mean?

2 comments

0

In this case it means players instance (client who clicked part)

0

Does that mean the player or the character?

1 answer

✓ Accepted answer

MouseClick is an event that fires when a player clicks on the parent Part of the ClickDetector. It provides the player that clicked it as a parameter.

In form it would look like this:

local clickDetector = script.Parent

--Include the parameter
function onClicked(PlayerThatClicked)
	print(PlayerThatClicked.Name) --print's name of the player that clicked it.
end

--Connects to the function. Same as your code too
clickDetector.MouseClick:Connect(onClicked)

Not all events have a parameter that do the same thing. You could check by using the API reference in RobloxDev site.

All parameters, like a variable, can use any name inside the parenthesis.

Arguments and Parameter article

8 comments

0

I believe you can just have PlayerThatClicked on line 5 without the .Name too.

0

I did test with both .Name and without but nothing got printed

0

Make sure this is in a 'Script' and that the script is inside of a ClickDetector. The ClickDetector should also be inside of a part.

0

I actually have a LocalScript in a ClickDetector which is in a part. Should I change to a normal script?

0

Thank you! I changed to a normal 'Script' and it worked!!

0

Yes. If you wanted the local script to detect the CD then you would need to put it in StarterPlayerScripts/StarterCharacter and then locate the part in workspace. A Script just makes it easier.

0

I placed my script, the one in my question, in a script, and then I just wondered, in the brackets is it the player or the character?

0

It is the player (instance).

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