How can I get the name of the player who clicked?

I recently started scripting. Today I’m trying to script a door with 2 buttons, wich can only be opened by the tycoon owner. At this point, I don’t really know how to get the name of the player who clicked one of the 2 buttons. Is there anyone who can help me with this?

local door = script.Parent.Parent.door
local buttons = {script.Parent.button1, script.Parent.button2}
local door_open = false


function click()
	local owner = tostring(script.Parent.Parent.Parent.Owner.Value)
	
	
	if door_open == false and owner == "Person_who_clicked" then
		door_open = true
		door.CanCollide = false
		door.Transparency = 0.75	
		for i, Part in pairs(buttons) do
			Part.BrickColor = BrickColor.new(37)

		end
	elseif door_open == true and owner == "Person_who_clicked" then
		door_open = false
		door.CanCollide = true
		door.Transparency = 0
		for i, Part in pairs(buttons) do
			Part.BrickColor = BrickColor.new(21)
		end
	end	
end

script.Parent.ClickDetector.MouseClick:Connect(click)

I think the “click” is the player who clicked, you can replace it with any name you want

script.Parent.ClickDetector.MouseClick:Connect(player)

Edit: then, try doing player.Name to check the player name, as the one I told you just returns the player who clicked.

If you use [ClickDetector.MouseClick], You can get player parameter who clicked.
You can use player same as variable only in this function

local door = script.Parent.Parent.door
local buttons = {script.Parent.button1, script.Parent.button2}
local door_open = false

script.Parent.ClickDetector.MouseClick:Connect(function(player) -- HERE WE GET PLAYER WHO CLICKED.
	local owner = tostring(script.Parent.Parent.Parent.Owner.Value)
	if door_open == false and owner == "Person_who_clicked" then
		door_open = true
		door.CanCollide = false
		door.Transparency = 0.75	
		for i, Part in pairs(buttons) do
			Part.BrickColor = BrickColor.new(37)

		end
	elseif door_open == true and owner == "Person_who_clicked" then
		door_open = false
		door.CanCollide = true
		door.Transparency = 0
		for i, Part in pairs(buttons) do
			Part.BrickColor = BrickColor.new(21)
		end
	end	
end)

If this helped, please mark this post as Solution. Thank you!

4 Likes

Just make that:
function click(player)
MouseClick automatically supplies you with a player object, the player who clicked

1 Like

Thanks for helping me out guys! The script is now working as I wanted.

local door = script.Parent.Parent.door
local buttons = {script.Parent.button1, script.Parent.button2}
local door_open = false

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	local owner = script.Parent.Parent.Parent.Owner.Value

	if owner == player then
		if door_open == false then
			door_open = true
			door.CanCollide = false
			door.Transparency = 0.75	
			for i, Part in pairs(buttons) do
				Part.BrickColor = BrickColor.new(37)

			end
		elseif door_open == true then
			door_open = false
			door.CanCollide = true
			door.Transparency = 0
			for i, Part in pairs(buttons) do
				Part.BrickColor = BrickColor.new(21)
			end
		end	
	end
end)
2 Likes