Why isn't my whitelist script working as written?

Hello Developers,

Today I wrote a basic little script for a little project of mine, but I could not get it to work. I’ve tried tons of different examples, and none of them worked. Here is the script, for reference:

local Players = game:GetService("Players")

local allowed = "82738847"

local check = false



local cache = {}
function getUserIdFromUsername(name)
	-- First, check if the cache contains the name
	if cache[name] then return cache[name] end
	-- Second, check if the user is already connected to the server
	local player = Players:FindFirstChild(name)
	if player then
		cache[name] = player.UserId
		return player.UserId
	end 
	-- If all else fails, send a request
	local id
	pcall(function ()
		id = Players:GetUserIdFromNameAsync(name)
	end)
	cache[name] = id
	
	return id
	
	
end

local user = Players:GetUserIdFromNameAsync("name")

	
function checkForPerms()
	
	if (user == allowed) then 
		
		check = true
		print("Sound Perms")
		
	else
		
		check = false
		print("No perms")
		
	end
	end	

	
		

		

		
	


checkForPerms()
	


print(check)
print(allowed)
print(user)

Here is what I get in the output:

Please note that I am a novice scripter, so please bear with me if I don’t understand. An explanation would be nice as well.

Thank you so much! :grinning:

1 Like

That is due to this line:

local user = Players:GetUserIdFromNameAsync("name")

This line gets the UserId of the user with the username name, which is 20731.

I see. How would I fix it so it would work as written?

Uh not sure if this is what you need but you could do a PlayerAdded event and the player and do Player.Name so when a player joins the game, it will check their UserId

I have that already, I think. Not 100% sure though.

Remove the “” and it should work.

1 Like

I attempted to do that, I got an error of:

W000: Unknown Symbol ‘name’.

I would recommend not including a lot of empty lines in your script because it harms readability. Anyway, pass the player name in the checkForPerms() function. Then call the getUserIdFromUsername() function with the name passed.

1 Like

Could you explain this a bit more? Could you share an example? Sorry, I don’t understand completely.

Sure, here is some dummy code.

local function getUserIdFromUsername(name)
    -- code which gets user id
    return id
end

local function checkForPerms(name)
    local id = getUserIdFromUsername(name)
    -- insert rest of code
end

checkForPerms("FxllenCode")
1 Like

Thank you, that explained it much better, I shall try this now.

1 Like

I see how that works, thank you! My last question would be, how would I get the username of the latest person to join, as it cannot just run checkForPerms("FxllenCode").

Via the Players.PlayerAdded event.

Players.PlayerAdded:Connect(function(player)
    checkForPerms(player.Name)
end)
1 Like

That’s right. Thank you for linking the documentation, I’ll have to read through that. I’ll mark you post as a solution.

1 Like