ImageButton OnlyAdmins as Nametag Color Changer

Hello!
I have tried to make an ImageButton to change the player’s (Administrator) nametag color from white to red, and in the other way (red > white).
So I have created an ImageButton and a LocalScript which manages it:

BUTTON.MouseButton1Down:Connect(function()
	if table.find(STAFF_USERNAMES, player.Name) then
	 if NAMETAG_DISABLED == true then --"NAMETAG_DISABLED" is just a variable in the script
		 NAMETAG_DISABLED = false
		 EVENTEN:FireServer(player) --Supposed to fire the event placed in ReplicatedStorage
	 elseif NAMETAG_DISABLED == false then
		 NAMETAG_DISABLED = true
		 EVENTDI:FireServer(player)
	 end
	else
		player:Kick("You are not allowed to use this feature. Did you see this button by error? Do not hesitate to report a potential bug.")
	end
end)

And, Server-Side:

local RS = game:GetService("ReplicatedStorage")

local EVENTEN = RS.PLAYER_MANAGEMENT:WaitForChild("P_NT")
local EVENTDI = RS.PLAYER_MANAGEMENT:WaitForChild("P_NT")

local function ONSERVER(player)
	player.Head:FindFirstChild("NAMETAG").PlayerName.TextColor3 = Color3.new(1, 0, 0.0156863)
end
local function ONSERVERD(player)
	player.Head:FindFirstChild("NAMETAG").PlayerName.TextColor3 = Color3.new(0, 0, 0)
end


EVENTEN.OnServerEvent:Connect(ONSERVER)
EVENTDI.OnServerEvent:Connect(ONSERVERD)

The Outpout does not show any error. I have tried to put a print("clicked") just after BUTTON.MouseButton1Down:Connect(function() but that doesn’t print anything.
I don’t really understand. Can you help me please?
Thank you.

Try MouseButton1Click instead? I don’t use MouseButton1Down, so I don’t know how different it is from that.

I just tried with MouseButton1Click but that doesn’t seem to work neither.

My only guess is that the event isn’t firing because BUTTON isn’t the button.
As far as I can tell it should work otherwise.

The button seems to be correctly parented with the UI.
image
local BUTTON = script.Parent.ImageButton
Does this function work with ImageButtons?

It should work.
Maybe the code runs before the button loads? Try a WaitForChild().

A couple of sorta off-topic things before we continue:

local EVENTEN = RS.PLAYER_MANAGEMENT:WaitForChild("P_NT")
local EVENTDI = RS.PLAYER_MANAGEMENT:WaitForChild("P_NT")

First of all, I fail to see the difference between these 2 variables. You should probably get rid of one and its event listener.

if NAMETAG_DISABLED == true then --"NAMETAG_DISABLED" is just a variable in the script
...
elseif NAMETAG_DISABLED == false then
...

The second elseif should be an else.

Alright, I think what might be happening is that you’re clicking on the Enabled image view that’s nested inside your ImageButton instead of the actual ImageButton. One of the other reasons might be that there’s a while loop inside the same script you showed a snippet of that runs before you connect your function to the MouseButton1Down event. If that’s the case, you should move the while loop at the very end of the script or use coroutines.

If that was the case, the script would crash :frowning:

Yes! It works.
As you said, I needed to put the loop below the MouseClick function to make it work properly.
Thank you guys!