You don’t need to use string.sub for this situation. The URL returns a table which contains the stats of IP. Instead, use .country with JSONDecode function. Also it would be odd if you were to use only the first five letters of the country.
So, I’ve started the search on what a “Java table” is, and came across this:
Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)}
};
Interesting, don’t you mean a JSON table?
I live in The Net her lan ds. Ahh, indeed, 5 words
Ah, a newly released service, the HttpsService
Not going to start a whole topic about why you should use GetService("Players"), but I can’t understand why you use PlayerAdded. I mean, the server location doesn’t change when someone joins (even if it’s the first user), that would be very strange…
Okay, thanks for acknowledging that it was nonsense to use PlayerAdded, but isn’t it an idea to fix an issue when you acknowledge an issue exists?
Don’t let this discourage you to contribute to the community. Consider dedicating more time to learn Luau.
As others have stated, there is no point in using PlayerAdded for this, the server location won’t change so you only need a single request per server.
Also, you’ve said a “Java” table when you meant a JSON table.
I’ll leave a better formatted script below with good practices, feel free to edit your post and use it.
-- All you have to do is create a StringValue in ReplicatedStorage with name "ServerLocation"
local HttpService = game:GetService("HttpService")
local Location = game:GetService("ReplicatedStorage"):WaitForChild("ServerLocation")
local Success, Response = pcall(function()
return HttpService:GetAsync("http://ip-api.com/json/")
end)
if Success then
Response = HttpService:JSONDecode(Response)
Location.Value = Response["country"]
end
You can also add more comments for beginners if you’d like to.
Hi, there’s an easier way to do this by just requesting it from ipconfig.io/country.
local uri = "https://ipconfig.io/country"
local httpsservice = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(plr)
wait(2) -- player loads fully
local getasyncinfo = httpsservice:GetAsync(uri)
game.Workspace.ServerLocation.Value = "Server Location: "..getasyncinfo
end)
Well, it does error if the value isn’t present, and you didn’t write anything about it in your topic either. This makes it extremely uncomfortable for less experienced programmers.
Here is a sample this will print the country that the server is in:
local HttpService = game:GetService("HttpService")
local URL = "http://ip-api.com/json/"
local JSONServerData
local ServerData
local ServerCountry
local Success,ErrorMessage = pcall(function() -- the script won't stop working at a line if its inside a pcall function
JSONServerData = HttpService:GetAsync(URL) -- roblox server will get info from that link
ServerData = HttpService:JSONDecode(JSONServerData) -- make the json into lua table, so its easy to use
ServerCountry = ServerData["country"] -- Get the country where the server is located
end)
if ErrorMessage ~= nil then -- if its not success script, it will print the error
print(ErrorMessage)
else
print(ServerCountry)
end
Sorry for the bump by the way I thought it would be helpful
I have tried this before with my game, it will return a false server location. You should use the timezone from the API instead of the city.
This is most likely how it is used in games:
local HttpService = game:GetService("HttpService")
local PlayerService = game:GetService("Players")
local API = "http://ip-api.com/json/"
local info = HttpService:JSONDecode(HttpService:GetAsync(API))
local function onPlayerAdded()
local success, errorMessage = pcall(function()
return
end)
if success then
print("Server Location: "..tostring(info["timezone"]))
end
if errorMessage then
warn(errorMessage)
end
end
PlayerService.PlayerAdded:Connect(onPlayerAdded)
Server Outcome:
Here is another outcoome of when I used remotes and user interface:
I’m not sure about it, because It always works for me, and this might be a aid to people who also got false location just like your faced problem, so anyway thanks for the info