generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Search by userId?

Previous Thread :: Next Thread 
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
05 Mar 2014 05:06 PM
Is there a way to search for a player's name if you have their userId, even if they are not in the game?
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 10:02 AM
??
Report Abuse
Vexedly is not online. Vexedly
Joined: 15 Sep 2006
Total Posts: 7011
06 Mar 2014 10:04 AM
[ Content Deleted ]
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 10:12 AM
I just looked by searching user Id but nothing about using it to search for a player. I'll keep looking, anyone else know how?
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
06 Mar 2014 10:14 AM
You may have to use HttpService to send a GET request to someone's server, which then gets the userId/username from the official Roblox site (we aren't allowed to send HTTP requests to the Roblox site). Let me see if he provides that.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
06 Mar 2014 10:27 AM
Yup.

http://www.robloxapi.com/

GET /Users/CurrentUsername.php
Gets the current username of user.
Parameters
UserId The ID of the user.


So you would send an HTTP request to:

"http://www.robloxapi.com/Users/CurrentUsername.php?UserId=" .. theUserId


This returns an encoded table with two fields: UserId and Username.
So you would do something like this ...


local HTTPService = game:GetService("HttpService")
function GetUsernameFromUserId(theUserId)

local result = HTTPService:GetAsync("http://www.robloxapi.com/Users/CurrentUsername.php?UserId="..theUserId)
print( "Username:", HTTPService:JSONDecode(result).Username )
return HTTPService:JSONDecode(result).Username
end

local myUsername = GetUsernameFromUserId(543545)
--> AgentFirefox
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 10:34 AM
Thank you very much, I'll try that :D
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 11:11 AM
The output says http requests are not enabled?
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
06 Mar 2014 11:55 AM
Right.
You have to insert the HttpService and set HttpEnabled to true.


Insert > Service > HttpService
Click on the HttpService instance in the Explorer.
Check the box next to the HttpEnabled property.
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 01:42 PM
Thank you. However, I think it only works once at the start of the server.

For instance, I entered my game online which has a global leaderboard (on a surface gui) that shows the top 5 players with the most goals. The leaderboard displayed that I had 3 goals, so the script worked by displaying my name and goals. But, then I scored and I should now have 4 goals, but the leaderboard didn't update, then after rejoining the game in a new server the leaderboard shown I had 4 goals.

Here's the leaderboard script and the output I got online from the remote error gui:

local ods = game:GetService("DataStoreService"):GetOrderedDataStore("PlayerGoals")

local HTTPService = game:GetService("HttpService")
function GetUsernameFromUserId(theUserId)

local result = HTTPService:GetAsync("http://www.robloxapi.com/Users/CurrentUsername.php?UserId="..theUserId)
print( "Username:", HTTPService:JSONDecode(result).Username )
UN = HTTPService:JSONDecode(result).Username
return HTTPService:JSONDecode(result).Username
end

function updateBoard(board, data)
for k,v in pairs(data) do
local pos = k
GetUsernameFromUserId(v.key)
local name = UN
local score = v.value
local nametextbox = board.SurfaceGui.Frame:FindFirstChild("Name" .. pos)
nametextbox.Text = name
local scoretextbox = board.SurfaceGui.Frame:FindFirstChild("Score" .. pos)
scoretextbox.Text = score
end
end

while true do
local pages = ods:GetSortedAsync(false, 5)
local data = pages:GetCurrentPage()
updateBoard(game.Workspace.Top5ScoreBoard, data)
--[[if not pages.IsFinished then
pages:AdvanceToNextPageAsync()
data = pages:GetCurrentPage()
updateBoard(game.Workspace.Next5ScoreBoard, data)
end]]
wait(1)
end

OUTPUT: Can't parse JSON - workspace.GoalsLeaderboard, line 7 - global GetUsernameFromUserId
- workspace.GoalsLeaderboard, line 15 - global updateBoard
- workspace.GoalsLeaderboard, line 28
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
06 Mar 2014 02:16 PM
Try printing 'theUserId' to see what its value is.
Where do you save the goals to the data store?
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 02:21 PM
I'm still abit unsure about this but I'm certain that the scope is global and the DataStore is called PlayerGoals. Every player's userId is a key in the data store and the value is the goals they have.

And, Does the remote error gui show print's, I can't test on build so that is the only way to see the output, right? I'll try anyway.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
06 Mar 2014 02:25 PM
I actually meant the script that saves the player's stats. Sorry for the confusion.
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 02:29 PM
Ah right this is the script:

debounce = true
function SaveStats()
if goal.Value == false then return end
if not debounce then return end
debounce = false

local Scorer = game.Players:findFirstChild("".. workspace.System.Ball.Team2LastKick.Value .."")

wait(1)

ds = game:GetService("DataStoreService"):GetOrderedDataStore("PlayerGoals")

function Change(input)
return input
end

if Scorer then
ds:UpdateAsync("".. Scorer.userId .."", Change)
ds:IncrementAsync("".. Scorer.userId .."", 1)
end

debounce = true

end

goal.Changed:connect(SaveStats)
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
06 Mar 2014 02:45 PM
I see nothing wrong with what you have, apart from something that would not be giving you this error.


local ods = game:GetService("DataStoreService"):GetOrderedDataStore("PlayerGoals")

local HTTPService = game:GetService("HttpService")
function GetUsernameFromUserId(theUserId)

local result = HTTPService:GetAsync("http://www.robloxapi.com/Users/CurrentUsername.php?UserId="..theUserId)
print( "Username:", HTTPService:JSONDecode(result).Username )
--UN = HTTPService:JSONDecode(result).Username < do not use global variables
-- we return for a reason
return HTTPService:JSONDecode(result).Username
end

function updateBoard(board, data)
for k,v in pairs(data) do
local pos = k
local name = GetUsernameFromUserId(v.key) -- name = the return value from the function
local score = v.value
local nametextbox = board.SurfaceGui.Frame:FindFirstChild("Name" .. pos)
nametextbox.Text = name
local scoretextbox = board.SurfaceGui.Frame:FindFirstChild("Score" .. pos)
scoretextbox.Text = score
end
end



Find a way to see what 'theUserId' is at execution. Try creating a message and putting it in workspace. My guess is 'theUserId' doesn't contain a positive integer.
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 03:13 PM
Okay, I got it to print the userId and it was a positive integer and it was my userId. I corrected the script with your advice, which is here:

local ods = game:GetService("DataStoreService"):GetOrderedDataStore("PlayerGoals")

local HTTPService = game:GetService("HttpService")
function GetUsernameFromUserId(theUserId)
print(theUserId)
local result = HTTPService:GetAsync("http://www.robloxapi.com/Users/CurrentUsername.php?UserId="..theUserId)
print( "Username:", HTTPService:JSONDecode(result).Username )
local m = Instance.new("Message")
m.Text = "".. theUserId ..""
m.Parent = workspace
wait(20)
m:remove()
return HTTPService:JSONDecode(result).Username
end

function updateBoard(board, data)
for k,v in pairs(data) do
local pos = k
local name = GetUsernameFromUserId(v.key)
local score = v.value
local nametextbox = board.SurfaceGui.Frame:FindFirstChild("Name" .. pos)
nametextbox.Text = name
local scoretextbox = board.SurfaceGui.Frame:FindFirstChild("Score" .. pos)
scoretextbox.Text = score
end
end

while true do
local pages = ods:GetSortedAsync(false, 5)
local data = pages:GetCurrentPage()
updateBoard(game.Workspace.Top5ScoreBoard, data)
--[[if not pages.IsFinished then
pages:AdvanceToNextPageAsync()
data = pages:GetCurrentPage()
updateBoard(game.Workspace.Next5ScoreBoard, data)
end]]
wait(1)
end
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
06 Mar 2014 03:14 PM
And you're still getting that error? :c
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 03:18 PM
Yeah, and when I disable and enabled the script using the remote error gui it works again (only once) and then breaks. But you can keep doing this to work it over and over.
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 04:07 PM
I know that before I had the function GetUsernameFromUserId() part, the leaderboard updated continuously without breaking so this part must be causing it to break. I have no clue what 'can't parse JSON' means.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
06 Mar 2014 04:10 PM
It means the output is invalid for JSON parsing.
Try printing the result instead of the JSON (this is before my print statement in the function).
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 04:29 PM
This is interesting. Btw, I've been testing on my alt so that's why it's a different name.

First time round:

{"UserId":"53358960", "Username":"BrazilWC2014"}

Second time round:

{"UserId":"sam4lifeGoals","Username":false}

I think I've found the problem, originally I didn't save the key as the userId it was "".. player.Name .."Goals", but that key is still in the DataStore so obviously this is not a userId... Will I need to create a new DataStore so there are only userId's in this one?
Report Abuse
sam4life is not online. sam4life
Joined: 17 Feb 2009
Total Posts: 1733
06 Mar 2014 04:48 PM
That's seem to have fixed it, thank you so much for the help :)
Report Abuse
Duelingwarlord is not online. Duelingwarlord
Joined: 15 Feb 2010
Total Posts: 22333
06 Mar 2014 04:49 PM
http://www.roblox.com/Get-Username-From-UserID-item?id=116836115
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image