Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago

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.

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

This is what the API would show for my username.

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

Any help? Thanks!

0
You do not need to do this, you could just use game.Players:GetUserIdFromUsernameAsync() and vice versa TheDeadlyPanther 2460 — 9y
0
Ok. Could you put a snippet of code, since I am not advanced with this stuff yet? Akward12345 0 — 9y
0
He did: game.Players:GetUserIdFromUsernameAsync() JamesLWalker 297 — 9y

1 answer

Log in to vote
2
Answered by 9 years ago

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:

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

Answer this question