How can I make a button (Clickable part) that only certain players can interact with?

I am wondering if anyone can help me with making a button that only certain players can click. The buttons are for an elevator and I want one specific floor only available to certain people. The buttons basically teleport the players to the different floors.

2 Likes

If you’re talking about using ClickDetectors, then just listen for the click event on the client, then use a remote event to teleport them

Yea I am but I am not a programmer yet, I am still learning. So I don’t know how to use those things.

1 Like

I don’t think you can ask for entire scripts on here, pretty sure this category is for asking for help with already existing scripts. You can try the recruitment category though

I am simply asking for an example dude. I know how to use the dev forum.

You could do something like this:

clickdetector = script.Parent --Change this to the ClickDetector
part = game.Workspace.TeleportPad --The object that you want to teleport to

clickdetector.MouseClick:Connect(function(player) --Detects when the button gets clicked
    if player.Name = “Username” then --The person that clicked
        player.Character:MoveTo(part.Position) --Moves the character
    end
end)

Hope this helps!

3 Likes

Hello!

Welcome to the scripting side of the Roblox Community!

So I have read your post, and this is something that’s not too hard to solve. I would proceed with creating a table that holds the eligible players. You could also use DataStores, which will also include a table.

Here’s an example of what I mean:

ServerSided Script inside the button

local EligiblePlayers = {"Your Name Here", "Someone's Name Here", ""}

script.Parent.MouseClick:Connect(function(Player))
for _, EligiblePlayer in pairs(EligiblePlayers) do
if Player.Name == EligiblePlayer then
--Code
end
end
end)

If you so wish to use DataStores instead, you could do something like this:

ServerSided Script inside ServerScriptService

local DataStoreService = game:GetService("DataStoreService")
local EligiblePlayersData = DataStoreService:GetDataStore("EligiblePlayers")

local Button = workspace.BUTTONS_NAME_HERE

EligiblePlayersData:SetAsync("ElevatorFloor1", 
   {
      ["Name"] = USER_ID,
      ["Name2"] = USER_ID2
   }
)

Button.MouseClick:Connect(function(Player))
local getEligiblePlayers = EligiblePlayersData:GetAsync("ElevatorFloor1")
for _, EP in pairs(getEligiblePlayers) do
if Player.Name == EP then
--Code
end
end
end)

Using the DataStore method might result in throttled requests if someone decides to spam the button, so adding a cooldown would be advised.

local cooldownEnabled = false

Button.MouseClick:Connect(function(Player))
if not cooldownEnabled then
cooldownEnabled = true
local getEligiblePlayers = EligiblePlayersData:GetAsync("ElevatorFloor1")
for _, EP in pairs(getEligiblePlayers) do
if Player.Name == EP then
--Code
end
spawn(function()) -- this is to prevent the rest of the code to stop
wait(1)
cooldownEnabled = false
end)
end
end
end)

I really hope this helped somehow. I did not write this code in studio, so please proceed with caution in case something’s not right here!

You could also go ahead and read about ClickDetectors here:

Have a great day / night, and good luck with proceeding your dream of becoming a scripter!

2 Likes

Thanks man. I really appreciate the help. :smile:

1 Like

No problem!

Feel free to message me if something ends up breaking! I would love to help you throughout your career! :+1: