Argument 1 missing or nil - RNG Spin Script

im trying to create a spin/roll mechanic where the player rolls for auras. however, my script is failing due to the error: “Argument 1 missing or nil” on line 41 of this script:

local ServerStorage = game:GetService("ServerStorage")
local RepStorage = game:GetService("ReplicatedStorage")

local rollEvent = RepStorage.Remotes:WaitForChild("Roll")
local showRollResultEvent = RepStorage.Remotes:WaitForChild("ShowRollResult")
local updateInventoryEvent = RepStorage.Remotes:WaitForChild("updateInventory")


local chances = {
	
	["TwoDudesKissing"] = 10;
	["Fire"] = 50;
	["Fortnite"] = 100;
	
}

local function roll(player)
	
	local playerDataFolder = ServerStorage:WaitForChild("PlayerData"):WaitForChild(player.UserId)
	local inventoryFolder = playerDataFolder:WaitForChild("Inventory")
	
	local listOfResults = {}
	
	for count = 1, 5, 1 do
		local randomNumber = math.random(1, 1000)
		local counter = 0
		
		for effect, weight in pairs(chances) do
			counter = counter + weight
			if randomNumber <= counter then
				table.insert(listOfResults, effect)
				break
			end
		end
	end
	
	showRollResultEvent:FireClient(player, listOfResults)
	
	task.wait(3.25)
	
	if inventoryFolder:FindFirstChild(listOfResults[5]) then
		inventoryFolder:WaitForChild(listOfResults[5]).Value += 1
	else
		local effect = Instance.new("IntValue")
		effect.Name = listOfResults[5]
		effect.Value = 1
		effect.Parent = inventoryFolder

	end
	
	updateInventoryEvent:FireClient(player, listOfResults[5])
	
end

rollEvent.OnServerEvent:Connect(roll)

can you please highlight the line that’s erroring since there’s no lines here

sdnjfweijwojgwiegjiewfewjfowienfgiowgngiwneg my fault

The cap for random number generation is > the total weight of your auras, so there’s a high chance the number is too big and nothing will be chosen. Moreover, just because you’re running the loop 5 times doesn’t mean 5 things will be added to the list. What I would do is just make the cap == the total. Also remove the break because it would end the loop straight away if count is larger

for count = 1, 5, 1 do
	local randomNumber = math.random(0, 160)
	local counter = 0
	for effect, weight in pairs(chances) do
		counter += weight
		if randomNumber <= counter then
			table.Insert(listOfResults, effect)
			continue
		end
	end
    --Failsafe, optional statement
    if listOfResults[count] == nil then
      table.insert(listOfResults,math.random(1,#chances))
    end
end

thanks for the fix; however, now my roll system lands on the same aura over and over even though it spins through the rest

two dudes kissing??? that’s my favorite

anyway

i think you need to break the for loop after rolling in order for it to be random everytime

lmaoo. appreciate the help. i realized that and fixed it like a minute after sending that reply

1 Like

Mark this as solved

Men. Men. Men. Men. Men.