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)
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 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.
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")
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").