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)
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!
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)