How to send the player to a function

This is in a server script:

local function spawninspectordocument(player)

    print(player.Name.." clicked!")

end

ClickDetector.MouseClick:Connect(spawninspectordocument)

Simple script, I just have no clue how to pass the player through to the function. For those asking why can’t you do a straight link (such as ClickDetector.MouseClick:Connect(function(player)), etc, this has to be a local function because I am also going to be calling it from another server script.

1 Like

You can do the following:

ClickDetector.MouseClick:Connect(function()
     spawninspectordocument(insert player here)
end)
1 Like

Thanks!

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz30

1 Like

The example from @Dragonfable6000 would work, as long as you remember to include player as a parameter at (function(player), but that implementation would make more sense if there was additional data you wanted to send through at the same time.

If you were only wanting to send through the player that activated the ClickDetector to the function, the code included in the original post would already achieve that. To showcase this, here’s a demonstration that utilizes both ways:

Example code and video

local ClickDetector = script.Parent

local function spawninspectordocument(player)
	print(player.Name.." clicked!")
-- This should print out twice if both options work
end

ClickDetector.MouseClick:Connect(spawninspectordocument)

ClickDetector.MouseClick:Connect(function(player)
	spawninspectordocument(player)
end)

2 Likes

Thanks for your extra work! I didn’t know the player could still be passed through, but I also have two other parameters that need to be passed through along with the player. I have it working still.

1 Like

Yup! It happens implicitly / automatically without needing to write anything extra when directly connected.

Oh in that case, then creating an anonymous function would be the better option of the two to go with, since you can’t send through additional data when directly connecting the event to the function.

ClickDetector.MouseClick:Connect(function(player)
	spawninspectordocument(player, extraDataHere)
end)

No problem! :smiley: Just wanted to make sure I provided some clarification about the different ways it can work so that yourself and anyone else who ends up reading this thread isn’t accidentally misled into thinking that only the anonymous function option would work, when both work well for different use cases.