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

Help with ClickDetector?

I'm a little rusty on Lua and I forgot how to get the name of the player from a ClickDetector

This is all I got

 function onClicked(click)
	

	if click.Parent.Name == "Player1" then 

		print("worked")

	else return

	end


end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Any help? Kinda clueless...

1 answer

✓ Accepted answer

Actually you're not that far off!

Typically I like to call the function right off, so this is what I'll do.

In Click functions, the argument is actually the player itself!

script.Parent.ClickDetector.MouseClick:connect(function(click)-- This is a more efficient way of calling your function!
    if click.Name == "Player1" then -- What you were doing was asking for the game.Players's name!

        print("worked")

    else return

    end
end)

4 comments

0

Thank you!

0

Wouldn't "click" return a nil value on line 2 since you named the player argument "Click" on line 1?

0

@minikitkat @m39 Yup, Error on line 2. Capitalize your letters appropriately please.

0

Alright I updated my answer! Thanks for pointing that out!

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