Random chances not working

So I want to make this spawner system but there’s a problem: something is going wrong.
Every time after the first time, the spawner just begins spawning minigunners.
Not only minigunners are the rarest item to be dropped, the “base” variable does not match at all with the random chances.
Spawner script:

local module = game:GetService("ServerStorage"):WaitForChild("WeaponList")
local modulescript = require(module)
local itemtable = modulescript.RandomizeWeapon(chances)
local spawnlist = modulescript.ItemList(List)

local handles = game.ServerStorage:WaitForChild("HandleList")
local item
local spawned = false
local counter = 0
local runcounter = 0
local function randomzier(randomitem)
	local base = math.random(1,100)
warn(base)
for i,v in pairs(itemtable) do
		counter = counter + v
		if base <= counter then
			print(tostring(i))
			item = spawnlist[i]
			print(item)
			local tool = handles:FindFirstChild(item)
			if not tool then
				warn("Error 402: Handle can't be found")
				return
			end
			if spawned == false then
			local clone = tool:Clone()
			clone.Position = script.Parent.Position
			clone.Parent = script.Parent
			spawned = true
		end
	end
		end
end
while true do
	runcounter +=1
	randomzier()

	print(runcounter)
	task.wait(10)
	if script.Parent:FindFirstChildOfClass("Part") or script.Parent:FindFirstChildOfClass("UnionOperation") then
		print(runcounter)
		else
		spawned = false
	end
end

Spawner module:

local spawning = {}

function spawning.RandomizeWeapon(chances)
	local randomizeditems = {
		4;
		6;
		10;
		15;
	25;
	40;
	}
	chances = randomizeditems
	return chances
end
function spawning.ItemList(List)
	local spawnlist = {
		"Minigun",
		"Grenade",
		"AK47",
		"GreaseGun",
		"Glock",
		"Sniper"
	}
	List = spawnlist
	return List
end
return spawning

Can you tell me what the print statements are printing?

Hello, I believe I see the issue here. After calling the randomizer function, you are not resetting the “counter” variable, which is a global variable in this script, meaning that it will keep going up for every randomizer() function call. Make sure you reset this variable when/before calling the function.

1 Like

Thank you so much! I was struggling so much trying to find the issue

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.