Hello! I’m trying to get data from an API but can’t get it to work.
This is the code:
game.Players.PlayerAdded:Connect(function(plr)
-- Gets HttpService and URL
local HS = game:GetService("HttpService")
local URL_API = "https://api.serverside.top/rank?robloxid=" .. plr.UserId
print(plr.UserId)
-- Makes the request to our endpoint URL
local response = HS:GetAsync(URL_API)
-- Parses the JSON response
local data = HS:JSONDecode(response)
-- Information in the data table is dependent on the response JSON
if data.message == "OK" then
local rank = script.Parent.TextLabel
rank.Text = data.rank
print(data.rank)
end
end)
Can you please give us some more information? What response code did you get? Did you get any errors? This is information we need in order to help you.
Well, the DevForum is not a spoon-feeding platform. We usually write code to give examples, not to rewrite another person’s whole script. Best we can do is help you out.
Now, what I care about is the status code, as well as all of the data. When you print data.rank, do you get anything back, or is it nil? Also, does the if data.message == "OK" then statement ever hold true? You could check that by printing literally anything right after it to see if the service does return “OK.”
This is the new code. I’m not getting any errors or an output at all.
game.Players.PlayerAdded:Connect(function(plr)
-- Gets HttpService and URL
local HS = game:GetService("HttpService")
local URL_API = "https://api.serverside.top/rank?robloxid=" .. plr.UserId
print(plr.UserId)
-- Makes the request to our endpoint URL
local response = HS:GetAsync(URL_API)
-- Parses the JSON response
local data = HS:JSONDecode(response)
-- Information in the data table is dependent on the response JSON
print(data)
if data.message == "OK" then
print("Data message OK")
local rank = script.Parent.TextLabel
rank.Text = data.rank
print(data.rank)
end
end)
Okay, so, I’ll make some assumptions while writing this reply. First and foremost, you have HTTPService enabled for Studio. Secondly, this is not a local script. Third, the game.Players.PlayerAdded function is not nested within an event or anything, it runs normally when the game starts. So, with those assumptions out of the way, let me dive a bit deeper.
I ran a dummy request on my UserId and I got the following response:
Code-wise, everything should be working. GetRequest() will return an error if the status code is anything other than 200, so everything should be working.
Please tell me that you aren’t running this in a local script. HTTPService can only be ran on the server. Additionally, the PlayerAdded event will not fire for yourself since your player has already been added to the game.
It will be under PlayerGui. You should never do stuff to StarterGui unless you intend for that stuff to actually happen to everyone. Use PlayerGui instead. Create a script in ServerScriptService and use the following code:
local HS = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(plr)
-- Gets HttpService and URL
local URL_API = "https://api.serverside.top/rank?robloxid=" .. plr.UserId
print(plr.UserId)
-- Makes the request to our endpoint URL
local response = HS:GetAsync(URL_API)
-- Parses the JSON response
local data = HS:JSONDecode(response)
-- Information in the data table is dependent on the response JSON
if data.rank then
print("Running")
local RankLabel = plr.PlayerGui:WaitForChild("UI"):WaitForChild("MainFrame"):WaitForChild("Frame"):WaitForChild("home"):WaitForChild("rank"):WaitForChild("TextLabel")
RankLabel.Text = data.rank
print(data.rank)
end
end)
Here are some things I would like to point out. First and foremost, I moved HttpService outside the PlayerAdded event. You don’t need to get the service every single time. This is a waste of resources. Secondly, you can see how I am accessing the TextLabel from inside the server script. Yes, it is a WaitForChild() mess, ideally, you’d wait for the UI to finish loading and then you’d modify it, but this will do for now. The rest of your code is okay. It does work as intended. Try it and let me know.
Another thing I want to add here is that you should remove the local script and also turn off ResetOnSpawn. If you wish to leave it on, then make sure you accommodate for new UIs by listening on the server for when the player will respawn and then modifying the UI again.
I’m sorry for your time and thank you for your help but I found a different solution using OnServerEvent. Again, I’m very sorry for wasting your time, I’ll give you the solution mark though.
local event = script.Parent.RemoteEvent
-- Gets HttpService and URL
event.OnServerEvent:Connect(function(plr)
local HS = game:GetService("HttpService")
local URL_API = "https://api.serverside.top/rank?robloxid=" .. plr.UserId
print(plr.UserId)
-- Makes the request to our endpoint URL
print(URL_API)
local response = HS:GetAsync(URL_API)
-- Parses the JSON response
local data = HS:JSONDecode(response)
-- Information in the data table is dependent on the response JSON
print(data)
if data.message == "OK" then
print("Data message OK")
local rank = script.Parent.TextLabel
rank.Text = data.rank
print(data.rank)
end
end)
Well, the solution mark isn’t the issue here. Looking at the code right now, this doesn’t seem like it will work even though it does work, it is a little bit of a forbidden way to go about it. Although RemoteEvents do seem like a good way to go about it, it is wrongly implemented here.
OnServerEvent runs on the server. This means that your script must be a server script.
This indicates a local relationship (as in, it is under the GUI itself). Although it does technically work, this is very bad practice. You shouldn’t treat server scripts like local scripts as this can bite you in the butt in the future. I did test it this way and it does seem to work, but do be mindful in the future when using methods like these, this is one of those methods that is discouraged by developers due to its nature. Of course, you are a beginner so it is totally understandable to do something like that, but do keep it in mind when making stuff in the future. Regardless, I’m glad that you found a solution to your problem, but I do encourage you to at least try to understand the approach that I recommended (no need to implement it or use it, but try to understand how to works so you get a better understanding of the server - client relationship).