ServerScriptService.HatModule:176: invalid argument #1 to 'random' (interval is empty)

Hello! i have a problem because i get this error: ServerScriptService.HatModule:176: invalid argument #1 to 'random' (interval is empty)
It is going off my HatModule module script.
Here is that ModuleScript:

local dataStore = game:GetService("DataStoreService")
local HatsData = dataStore:GetDataStore("HatDataS2savingg")
local module = {}


local currency = script:WaitForChild("Settings"):WaitForChild("Currency")
local maxhats = script:WaitForChild("Settings"):WaitForChild("MaxEquippableHats")
local cansavehats = false
local alredysavedhats = true

function module.loadhats(player)
		
	local maxhats = Instance.new("IntValue",player)
	maxhats.Name = "maxHats"
	maxhats.Value = script:WaitForChild("Settings"):WaitForChild("MaxEquippableHats").Value
	
	local maxhatsinv = Instance.new("IntValue",player)
	maxhatsinv.Name = "maxHatsInv"
	maxhatsinv.Value = script:WaitForChild("Settings"):WaitForChild("MaxHatStorage").Value
	
	local HatsFolder = Instance.new("Folder",player)
	HatsFolder.Name = "Hats" 
	local HatsTable = {}
	
	
	local function dataload()
	HatsTable = HatsData:GetAsync(player.UserId)
	end

	local sucess, err = pcall(dataload)

	if sucess then

   
	if not HatsTable or #HatsTable == 0 then
		alredysavedhats = false
	end



	local num1 = 1
	local num2 = 2
	local num3 = 3
	local num4 = 4


if HatsTable then
	if #HatsTable >0 then
	for i = 1, #HatsTable / 4 do
			 
					
					local hat = Instance.new("BoolValue",HatsFolder)
						hat.Name = HatsTable[num1]
						hat.Value = HatsTable[num2]
						
					local hatimage = Instance.new("NumberValue",hat)
						hatimage.Name = "ImageID"
						hatimage.Value = HatsTable[num3]
						
					local hatrarity = Instance.new("StringValue", hat)
						hatrarity.Name = "Rarity"
						hatrarity.Value = HatsTable[num4]
						 	
						
					if num4 == #HatsTable then
						cansavehats = true
					end
						
						
						num1 = num1 + 4
						num2 = num1 + 1
						num3 = num1 + 2
						num4 = num1 + 3
			
		end
	end
end
	else	
				print("Error while loading: "..err)
				player:Kick("Failed to load your data!")	
end
end

--Save Hats
function module.savehats(player)
	if cansavehats == true or alredysavedhats == false then
		
		local HatsTableSave =  {}
		
	for i,v in pairs(player:WaitForChild("Hats"):GetChildren()) do
		table.insert(HatsTableSave, v.Name)
		table.insert(HatsTableSave, v.Value)
		table.insert(HatsTableSave, v:WaitForChild("ImageID").Value)
		table.insert(HatsTableSave, v:WaitForChild("Rarity").Value)
	end

		local function checking()
			HatsData:SetAsync(player.UserId, HatsTableSave)
		end

		local sucess, err = pcall(checking)
		
		if sucess then
					
		else
			print("Error while saving: "..err)
			local plr = player
			module.savehats(plr)
		end
	end
end

function module.PlrJoinedAddHat(player)
	for i,v in pairs(player:WaitForChild("Hats"):GetChildren())do
		if v.Value == true then
			local clone = game.ServerStorage:WaitForChild("Hats"):WaitForChild(v.Name):Clone()
			clone.Name = v.Name
			
			local object = Instance.new("ObjectValue",v)
			object.Name = "Object"
			object.Value = clone
			
			repeat wait() until game.Workspace:FindFirstChild(player.Name)
			local char = game.Workspace:WaitForChild(player.Name)
			char:WaitForChild("Humanoid"):AddAccessory(clone)
		end
	end
end

function module.deletehat(player,hat)
	hat:Destroy()
end

function module.equipunequip(player,state,hat)
	if state == "Unequip" then
		hat.Value = false
		hat:WaitForChild("Object").Value:Destroy()
		hat:WaitForChild("Object"):Destroy()
		
	elseif state == "Equip" then
		hat.Value = true		
		
		local clone = game.ServerStorage:WaitForChild("Hats"):WaitForChild(hat.Name):Clone()
		clone.Name = hat.Name
		
		local object = Instance.new("ObjectValue",hat)
		object.Name = "Object"
		object.Value = clone
		
		repeat wait() until game.Workspace:FindFirstChild(player.Name)
		local char = game.Workspace:WaitForChild(player.Name)
		char:WaitForChild("Humanoid"):AddAccessory(clone)
	end
end



local hats = game.ServerStorage:WaitForChild("Hats")
function getRandomRarity(hatshop)
	local tempRarityTable = {}
	for _, i in ipairs(hatshop:WaitForChild("Rarities"):GetChildren()) do
		for p = 0, i.Value do
			table.insert(tempRarityTable, i.name)
		end
	end
	return tempRarityTable[math.random(#tempRarityTable)]
end

function getRandomHatByRarity(rarity)
	local tempTable = {}
	for _, i in ipairs(hats:GetChildren()) do
		if i:WaitForChild("Rarity").Value == rarity then
			table.insert(tempTable, i)
		end
	end
	return tempTable[math.random(#tempTable)]
end

function module.buyhat(plr, hatshop, amount)
	if hatshop == nil then return end
	
	local currency = game.ReplicatedStorage.data:WaitForChild(plr.UserId):WaitForChild("coins").Value
	local price = hatshop:WaitForChild("Price").Value
	if currency >= price then
		if #plr:WaitForChild("Hats"):GetChildren() + amount > script:WaitForChild("Settings"):WaitForChild("MaxHatStorage").Value then return "Not enough space" end
		currency = currency - price
		local hatsOpened = {}
		
		for i = 1, amount do
			local rarity = getRandomRarity(hatshop)
			local hat = getRandomHatByRarity(rarity)
			
			local hatAdd = Instance.new("BoolValue",plr:WaitForChild("Hats"))
				hatAdd.Name = hat.Name
				hatAdd.Value = false
			local rarit = Instance.new("StringValue",hatAdd)
				rarit.Name = "Rarity"
				rarit.Value = rarity
			local imageid = Instance.new("NumberValue",hatAdd)
				imageid.Name = "ImageID"
				imageid.Value = game.ServerStorage:WaitForChild("Hats"):WaitForChild(hat.Name):WaitForChild("ImageID").Value

			
			table.insert(hatsOpened, hatAdd.Name)
			table.insert(hatsOpened, rarit.Value)
			table.insert(hatsOpened, imageid.Value)
			
		end
		
		return hatsOpened
	else
		return "Insufficient Funds"
	end
end



return module

this error is from line 176

return tempTable[math.random(#tempTable)]

Thank you very much for your help!

Try

return tempTable[math.random(1,#tempTable)]
1 Like

@Redusofficial Then i get error: ServerScriptService.HatModule:176: invalid argument #2 to 'random' (interval is empty)

The reason you are getting an error is because right here:


What I highlighted is you returning math.random. It’s not written correctly.

Right now all that is doing is saying math.random(sizeoflist) but nothing else. Math.random requires two numbers, a starting one (startnum,endnum) but instead you just got one number which is why you are “missing an interval”.

To fix this simply do this, since I am assuming you want a hat in that table:

Replace:

return tempRarityTable[math.random(#tempRarityTable)]

With:

return tempRarityTable[math.random(1,#tempRarityTable)]

That way it will pick something from the rarity table from 1 (start of your table) to the size of your table (#tempRarityTable), so it will make one item from that to return.

This should work, let me know if you still receive an error.

He had the same issue from from this:

Also, wrong function.

As for me, it’s working fine. You may want to check whether your folders are correct, or something related to that.

Could that his #tempRarityTable is empty as well, check if it’s adding values to it properly or whether its nil.

#tempRarityTable isn’t used at all in getRandomHatByRarity, and it says his error is on line 176. Maybe he used the function incorrectly, or there are just no hats with the rarity that he specified: most likely the latter. An easy fix. OR the value inside of the hat was a number when in reality it should’ve been a string.

It does work though.
image
image
image

It’s an error with math.random missing an interval so it’s both of these that need to be replaced as math.random(1,[sizeoflist]):


Let me know after fixing those two if he gets anymore errors or what different errors he would be getting, but after that he shouldn’t be getting an error with missing intervals for math.random.

If you send one argument to math.random, it will automatically do math.random(1, n).

-- prints a random number between 1 and 5
print(math.random(5))

The problem here is that the length of the list is 0, which means no items are being added.