Why is this not registering the UserID for this special command?

The Goal

I want this to admin the people with the UserIds inside the table.

The Issue

It doesn’t register the UserId, nor does it give the user the permission to access this command.

What I tried

Nothing, as I don’t know where to edit nor the actual error in my script. I know I could link it to a username but Usernames change too often, hence why I’m aiming for the UserId.

The Script

--Made By MillerrIAm
--------Variables-------
local SFXSystem = require(script.Parent.SFXs)
local Admin = false
---------Admins--------
local admins = {678299}
--------Main Code------
game.Players.PlayerAdded:Connect(function(plr)
	for i,v in pairs (admins) do
		if v[plr.UserId] then
			Admin = true
		end
	end	
	plr.Chatted:Connect(function(msg)
		if Admin then
			if msg == "true" then
				SFXSystem.SFX1()
			end
		end
	end)
end)

Final Note

I also tried using v == plr.UserId and it gave the command to everyone…
Thank you to anyone who looks at this and attempts to help me solve this issue.

It looks like you are using a shared variable “Admin” For all users
So If the latest person to join the server has admin it will allow any user to run the command
v == plr.UserId should be the correct check
If you instead store the Admin variable under the Player Joined Event it will keep the variable local to that event

2 Likes

v == plr.UserId was correct, however, you need to move your local Admin = false inside the PlayerAdded function. This makes the variable only accessible to the current instance of the function; not every time the function is run.

2 Likes

First of all, admins is going to be an array, (keys of 1, 2, 3, etc.) so you can change pairs to ipairs.

Second, i is the key and v is the value, so you want to say v == plr.UserId.

Third, you have local variable Admin outside of the PlayerAdded event function, so if one person is an admin and sets that variable to true, it will stay true and all following players who join will be admin. You want to move this variable into the function so that there will always be a new Admin variable (that equals false) whenever the function is ran.

3 Likes