Code not working

I want to make the HQButton visible only to those whose userid is mentioned in the code, but it is not working as it should.
If the properties of the button is set to true, then it shows the button to all the players and does not care if the ID is mentioned in code or not.
If the properties of the button is set to false, then it doesn’t show the button even if the player’s name is mentioned in the code.

Code-

local player = game.Players.LocalPlayer
local HQButton = script.Parent
local DepartmentFrame = HQButton.Parent
local ChooseFrame = DepartmentFrame.Parent
local EROIStaffSystemUI = ChooseFrame.Parent
local HQLoginFrame = EROIStaffSystemUI:WaitForChild("HQLoginFrame")

local allowedUserIds = {
	1806979894, -- Abhaas
}

local function isAllowedPlayer()
	for _, userId in ipairs(allowedUserIds) do
		if player.UserId == userId then
			return true
		end
	end
	return false
end

if isAllowedPlayer() then
	HQButton.Visible = true
else
	HQButton.Visible = false
end

local function onHQButtonClicked()
	ChooseFrame.Visible = false
	HQLoginFrame.Visible = true
end

HQButton.MouseButton1Click:Connect(onHQButtonClicked)

UI structure-

image

The HQButton visibility is set to false

Try using local scripts and not server scripts.
For this to work, you also need to make sure that ChooseFrame/DepartmentFrame are visible.

1 Like

I would recommend inverting your system a little bit - have the GUI be in ServerStorage, then have a server script which listens to PlayerAdded. If the new player is authorised, then clone the GUI into the player’s PlayerGui (Player.PlayerGui).

This ensures that only people are meant to have the UI have it, and can help prevent against exploiters accessing it. This would also allow you to get rid of some of the verification logic from the GUI’s script, which is currently causing you problems

2 Likes

Both are working, but I prefer the @CoderGoldy solution more. Thanks :pray: