Invalid argument #2 to 'random' (interval is empty)


local Model = workspace["2R6"]
local hats = workspace.HATS:GetChildren()
local items = workspace.ITEMS:GetChildren()
local randomitem = workspace.ITEMS
local thumbnailUrl = nil
local MarketplaceService = game:GetService("MarketplaceService")
local SSS = game:GetService("ServerScriptService")
local module = require(SSS.Items)
local ITEMRAP = 0
local player = script.Parent.Parent.Parent.Parent.Parent
local buying = 1
local selling = 0
local hatsowned = player.Backpack:GetChildren()
local bp = player.Backpack
local selected = 1
local itemID = 1337
local randomHat = 1337
local alrdone = true

local clonedhat

function customerchange ()

	wait()
	print("detected")
	wait()
	randomHat = bp:GetChildren()[math.random(1, #bp:GetChildren())]
	randomitem = hats[math.random(1, #hats)]
	print(randomHat.Name)
	itemID = randomHat.Name
	
	
	local itprdct = MarketplaceService:GetProductInfo(randomHat.Name)
	local img = script.Parent
	clonedhat = randomitem:Clone()
	script.Parent.Parent:WaitForChild("nameofitem").Text = itprdct.Name
	thumbnailUrl = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. itemID .. "&width=420&height=420&format=png"
	img.Image = thumbnailUrl
	Model.Humanoid:AddAccessory(clonedhat)
	Model.Head.BrickColor = BrickColor.random()
	Model.Torso.BrickColor = BrickColor.random()
	Model["Left Arm"].BrickColor = BrickColor.random()
	Model["Left Leg"].BrickColor = BrickColor.random()
	Model["Right Arm"].BrickColor = BrickColor.random()
	Model["Right Leg"].BrickColor = BrickColor.random()
end

it says this: invalid argument #2 to ‘random’ (interval is empty)

help how can i fix this

Which random?

chaaaaaaaaaars

Make sure that theres atleast one object in the list


randomHat = bp:GetChildren()[math.random(#bp:GetChildren())]
randomitem = hats[math.random(#hats)]|

Can you try this?

The reason this happens is because your list is empty. Make sure there are multiple objects inside it.