Attempt to iterate over a nil value

Hello,

I am making an outfit loader game and I keep getting this error:

Here is the script:

local event = game.ReplicatedStorage:WaitForChild("RequestUser")
local plr = game.Players.LocalPlayer

local inputbox = script.Parent.Parent.InputBox
local lastRequested

local debounce = false

script.Parent.MouseButton1Click:Connect(function()
	if inputbox.Text ~= "" then
		inputbox.Parent.Visible = false
		if inputbox.Text ~= lastRequested then
			debounce = true
			lastRequested = inputbox.Text
			local stringReceived = event:InvokeServer(inputbox.Text)
			
			if stringReceived then
				script.Parent.Text = stringReceived
				task.wait(1)
				script.Parent.Text = "Request"
				debounce = false
			else
				task.wait(1)
				debounce = false
			end
		end
	end
end)

This is the error line:

local stringReceived = event:InvokeServer(inputbox.Text)

Any help is appreciated. Thanks.

5 Likes

Wrong script, please give us the one that handles the loop

6 Likes

That tells me there’s a remote function you’re invoking on the server, could we have a look at the remote function you’ve got set up on the server please? :slight_smile:

5 Likes

Sorry, this is the script:

local httpService = require(game.ServerStorage.HTTP)
local baseUrl1 = "https://avatar.roblox.com/v1/users/"
local baseUrl2 = "/outfits"
local requests = {}

local Players = game:GetService("Players")

local GuiPart = workspace.GuiPart
local Frame = GuiPart.SurfaceGui.Frame
local PlayerNameText = Frame.BottomFrame.PlayerName
local PlayerIconImage = Frame.MiddleFrame.PlayerIcon

local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420

local QueuePart = workspace.QueuePart
local QueueScroll = QueuePart.SurfaceGui.Frame.ScrollingFrame

local QueueTemplate = script.Template

local CurrentRequested

local checkPlayer = function()
	if #requests >= 1 then
		local userId = (requests[1])
		local data = httpService.getasync(baseUrl1..userId..baseUrl2)
		if data then
			CurrentRequested = userId
			local name
			pcall(function ()
				name = Players:GetNameFromUserIdAsync(userId)
			end)
			local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

			PlayerNameText.Text = name
			PlayerIconImage.Image = content


			game.Workspace.Map.Outfits:ClearAllChildren()
			local outfits = data.data
			for i, v in outfits do
				local desc = game.Players:GetHumanoidDescriptionFromOutfitId(v.id)
				local c = game.ServerStorage.BaseAvatar:Clone()
				c.Name = v.name
				c.Parent = workspace.Map.Outfits
				c.HumanoidRootPart.Position = Vector3.new(workspace.StartingPoint.Position.X + (5 * (#workspace.Map.Outfits:GetChildren() - 1)), workspace.StartingPoint.Position.Y + 2.5, workspace.StartingPoint.Position.Z)
				c.Humanoid.NameDisplayDistance = 50
				c.Humanoid:ApplyDescription(desc)
				c.HitBox.Position = c.HumanoidRootPart.Position
			end
			table.remove(requests, table.find(requests, userId))
			QueueScroll:FindFirstChild("1"):Destroy()
			local Items = 0
			for _, Child in ipairs(QueueScroll:GetChildren()) do
				if Child:IsA("Frame") then
					Items += 1
				end
			end
			
			for i = 1, Items do
				for _, Child in ipairs(QueueScroll:GetChildren()) do
					if Child:IsA("Frame") then
						Child.Name = i
					end
				end
			end
		end
	end
end


local loop = function()
	while task.wait(60) do
		if #requests >= 1 then
			checkPlayer()
		else
			repeat game:GetService("RunService").Heartbeat:Wait() until #requests >= 1
		end
	end
end

game.ReplicatedStorage.RequestUser.OnServerInvoke = function(plr, targetName)
	local targetUserId = game.Players:GetUserIdFromNameAsync(targetName)
	if targetUserId ~= nil then
		local isOnRequests = false
		for i, v in pairs(requests) do
			if v == targetUserId then
				isOnRequests = true
				break
			end
		end
		if isOnRequests == false then
			if not table.find(requests, targetUserId) then
			table.insert(requests, targetUserId)
		
			QueueScroll:ClearAllChildren()
			
			local LayoutClone = script.UIListLayout:Clone()
			LayoutClone.Parent = QueueScroll
			
			local DidAlready = {}
			for i = 1, #requests do
					print(#requests)
					print(i)
					if not table.find(DidAlready, requests[i]) then
						table.insert(DidAlready, requests[i])
						local PlayerThumbnail = Players:GetUserThumbnailAsync(requests[i], Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)

						local PlayerInfo = Players:GetNameFromUserIdAsync(requests[i])

					local NewTemplate = QueueTemplate:Clone()
					NewTemplate.Name = i
					NewTemplate.LayoutOrder = i
					NewTemplate.MainFrame.TextLabel.Text = PlayerInfo
					NewTemplate.PlayerIcon.Image = PlayerThumbnail
					NewTemplate.Visible = true
					NewTemplate.Parent = QueueScroll
						end
					end
				end
			if #requests == 1 then
				checkPlayer()
			end
			return "Queued"
		else
			return "Already Listed"
		end
	else
		return "Doesn't Exist"
	end
end



coroutine.wrap(loop)()
3 Likes

Do you mean this?

image

4 Likes

Nono, I mean the script. Looking at it now. :slight_smile:

2 Likes

What might help is breakpointing the code within the event, and tell us which line it gets to, requests should never be nil, as {} != nil. Put the breakpoint here, get the remote function to invoke and let me know (click step over at the top) and let us know which line it gets to and what the variable values are. :slight_smile:. The only two culprits I can think of are either of those for loops, probably because a request doesn’t exist in the request queue at the time of invoking.

3 Likes

Hello,
When it reaches line 122, it gives this error:


The line is:

checkPlayer()
2 Likes

When it gets to that line (line 122), click on step into instead. Then step over until it breaks again. What line does it take you to now? Should have thought about that haha.

2 Likes

It now breaks on line 41:

for i, v in outfits do

The error:

1 Like

try changing that to

local data = httpService:GetAsync(baseUrl1..userId..baseUrl2)
    if data and data.data then
2 Likes

You’re setting outfits to ‘data.data’ on line 40. I’m not sure whether ‘data’ is a member/key of ‘data’, Just above that line (‘that line’ being like 40), can you print out data and run the code again. Put what it prints out here please. :slight_smile:

1 Like

The ‘data’ variable exists/has something in it, otherwise it wouldn’t get into that ‘if data then’ check on line 27.

1 Like

It prints out:

1 Like

Replace line 40:

local outfits = data.data

with:

local outfits = data.response.data

Now it prints this:

This might be because you’ve queried the API too much (if that “Error fetching data:” print is still the print we put above line 40). Give it a few minutes and try again. :slight_smile:

Tried it again and it printed this:

2 Likes

Oh ok, thanks. Will this fix the “attempt to index nil with “data”” error?

2 Likes

What is ‘ServerScriptService.AvatarsLoading’? Am I able to take a look at this script please?

2 Likes