Function only clickable by certain players

Hi people,

I am trying to make a system where a click function can only be executed by certain players. For example, a click door or click alarm.

I would also like to make a GUI message pop up stating that the player cannot perform this action or the same message that hovers on the mouse.

I haven’t made an attempt because I’m just completely stuck and I don’t know how to start. If anyone could help, that would be great. Thanks!!

3 Likes

Can you specify :

i don’t know if it’s a click via ClickDetector (in Part) or a TextButton (in GUI)

1 Like

It’s via a ClickDetector in a Part. I could also use a hand with GUI things if you don’t mind.

Add a script inside the ClickDetector, and include the following code in it:

local AllowedPlayers = {39922681, 1} -- Add whoever you want to be able to click the ClickDetector (user id)

script.Parent.MouseClick:Connect(function(Player)
	local FoundInTable = false
	for _, userid in pairs(AllowedPlayers) do
		if Player.UserId == userid and FoundInTable == false then
			FoundInTable = true
		end
	end
	if FoundInTable == true then
		-- Player found in table, open door
	else
		-- Player wasn't found in table, show an error gui (preferably, you should fire a remote event to the player, and handle the gui effects on the client)
	end
end)
5 Likes

It appears that when I copied the exact same code and put a print function (print(‘Success’) to be exact) underneath the ‘FoundInTable == true’ line, nothing happened. Is is just a Studio problem?

EDIT: I found the problem, it turns out the ‘MouseClick’ isn’t a valid member of Part.

Okay, it all works now! But, if I were to make the error message on the GUI, I just need help with getting the script to display it for a few seconds and then remove it. The rest I’ll be able to handle myself.

Add a RemoteEvent in ReplicatedStorage and call it whatever you like. In this scenario, we’ll just call it ShowGui, you can rename it if you want.

Updated script of the ClickDetector:

local AllowedPlayers = {39922681, 1} -- Add whoever you want to be able to click the ClickDetector (user id)

script.Parent.MouseClick:Connect(function(Player)
	local FoundInTable = false
	for _, userid in pairs(AllowedPlayers) do
		if Player.UserId == userid and FoundInTable == false then
			FoundInTable = true
		end
	end
	if FoundInTable == true then
		-- Player found in table, open door
	else
		game.ReplicatedStorage.ShowGui:FireClient(Player)
	end
end)

Client script (local script):

game.ReplicatedStorage.ShowGui.OnClientEvent:Connect(function()
	script.Parent.Error.Visible = true
	wait(3) -- Time to wait before hiding the error text
	script.Parent.Error.Visible = false
end)

Setup:

You can obviously make the gui more cleaner and better by twening it in, adding some special effects with TweenService and stuff like that. You can do that if you like in the client script.

Another approach of showing an error gui, is to copy the gui, parent it to the player’s PlayerGui, and then delete it after a short time (all handled in the server) - but that will give away the option to customize your gui (add special effects, tween it in, etc.)

3 Likes

I see. But, if you’re making it a frame, is there anything you need to change (e.g. the position of the scripts and text)?

If you parented the text label to a frame, just simply do like: script.Parent.Frame.Error.

I see. Thanks for helping me once again!

1 Like