Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How does Username-To-ID Work? (How to get the ID into a script) [Done]

I have found a user-to-ID API. I do not know how to get the ID from it though. The code is as follows.

if (string.find(msg, "!promote") == 1) then --- msg starts with "!promote"
		-- words and numbers
		for word in msg:gmatch("%w+") do 
			if word ~= "!promote" then
				local p = matchPlayer(word)
				userid = game.HttpService:GetAsync("http://api.roblox.com/users/get-by-username?username="..p)
				
				if p ~= nil then
					promouser(userid,GROUPID,RANKID)
				end
			end
		end
end

This is what the API would show for my username.

{"Id":39234598,"Username":"Smaltin", "AvatarUri":null,"AvatarFinal":false,"IsOnline":false}

Any help? Thanks!

3 comments

0

You do not need to do this, you could just use game.Players:GetUserIdFromUsernameAsync() and vice versa

0

Ok. Could you put a snippet of code, since I am not advanced with this stuff yet?

0

He did: game.Players:GetUserIdFromUsernameAsync()

1 answer

✓ Accepted answer

One way of doing it is using a web API as you have there. But the simpler way of doing this is game.Players:GetUserIdFromNameAsync(p) with p being the player's name as a string. The reverse of this would be game.Players:GetNameFromUserIdAsync(p), again with p being the player's name as a string. With this put into your code it would be:

if (string.find(msg, "!promote") == 1) then --- msg starts with "!promote"
        -- words and numbers
        for word in msg:gmatch("%w+") do 
            if word ~= "!promote" then
                local p = matchPlayer(word) -- This returns a string?
                if p ~= nil then
                    userid = game.Players:GetUserIdFromNameAsync(p) -- Put here so doesn't error is p is nil
                    promouser(userid,GROUPID,RANKID)
                end
            end
        end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.