API issues, attempt to concatenate string with nil

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to use roproxy’s API to return places currently playing

  1. What is the issue? Include screenshots / videos if possible!

The issue is that I dont understand apis and my first time ever using it, The error is attempt to concatenate string with nil

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Most posts with solutions to “attempt to concatenate string with nil” are not related to API stuff which won’t help me in my current situation

local mapselection = script.Parent
local mapdetails = script.Parent.Parent.MapInfo
local description = mapdetails.Description
local name = mapdetails.Creator
local Image = mapdetails.MapImage
local playercount = mapdetails.PlayerCount
local play = mapdetails.PlayButton
local mainframe = script.Parent.Parent
local mapid = mainframe.MapID
local mapname = mapdetails.MapName


local teleportservice = game:GetService("TeleportService")
local message = game:GetService("MessagingService")
local http = game:GetService("HttpService")

local plr = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent




for _,button in pairs(mapselection.ScrollingFrame:GetChildren()) do
	if button:IsA("ImageButton") then
		button.MouseButton1Click:Connect(function()
			if game:GetService("ReplicatedStorage").MapInfo:FindFirstChild(button.Name) ~= nil then
				local text = require(game:GetService("ReplicatedStorage").MapInfo:FindFirstChild(button.Name))
				local playing = nil
				local success,result = pcall(function()
					playing = http:JSONDecode(http:GetAsync("https://games.roproxy.com/v1/games?universeIds="..tostring(text.playingid)))
				end)
				
				description.Text = text.Description
				name.Text = text.Name
				Image.Image = text.Image
				mapname.Text = button.Name
				mapid.Value = text.Id
				if success then
					playercount.Text = "Players currently playing: "..playing.data.playing -- error here, error is 'attempt to concatenate string with nil'
				else
					playercount.Text = "Error loading players."
					print(result)
				end
				mapdetails.Visible = true
			end
		end)
	end
end

play.MouseButton1Click:Connect(function()
	teleportservice:Teleport(mapid.Value,plr)
end)

1 Like

The error is telling you that playing.data.playing is nil. Have you tried printing playing to see what’s there?

It just prints the table : hexcode

There should be 3 dots on the output, click them and disable “Log Mode” and try again.

i don’t think you need to require here its not a module its for the text button right and this i think is the start of the issue on why its getting nil for the id

This helped me, thank you! I forgot to put a [1] after data which is the table I was meaning to go to. I was supposed to do result.data[1].playing instead of result.data.playing
New script

local mapselection = script.Parent
local mapdetails = script.Parent.Parent.MapInfo
local description = mapdetails.Description
local name = mapdetails.Creator
local Image = mapdetails.MapImage
local playercount = mapdetails.PlayerCount
local play = mapdetails.PlayButton
local mainframe = script.Parent.Parent
local mapid = mainframe.MapID
local mapname = mapdetails.MapName


local teleportservice = game:GetService("TeleportService")
local message = game:GetService("MessagingService")
local http = game:GetService("HttpService")

local plr = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent




for _,button in pairs(mapselection.ScrollingFrame:GetChildren()) do
	if button:IsA("ImageButton") then
		button.MouseButton1Click:Connect(function()
			if game:GetService("ReplicatedStorage").MapInfo:FindFirstChild(button.Name) ~= nil then
				local text = require(game:GetService("ReplicatedStorage").MapInfo:FindFirstChild(button.Name))
				local success,result = pcall(function()
					return http:JSONDecode(http:GetAsync("https://games.roproxy.com/v1/games?universeIds="..tostring(text.playingid)))
				end)
				
				description.Text = text.Description
				name.Text = text.Name
				Image.Image = text.Image
				mapname.Text = button.Name
				mapid.Value = text.Id
				if success then
					playercount.Text = "Players currently playing: "..result.data[1].playing 
				else
					
					playercount.Text = "Error loading players."
					print(result)
				end
				mapdetails.Visible = true
			end
		end)
	end
end

play.MouseButton1Click:Connect(function()
	teleportservice:Teleport(mapid.Value,plr)
end)