Is there a better way of doing this?

script:

	if game.ServerStorage.Day.Value <= 2 then
					number = math.random(0,3)
				
				elseif game.ServerStorage.Day.Value <= 5 and game.ServerStorage.Day.Value >= 2 then
					number = math.random(0,5)
				end
				if number == 0 then

				elseif number == 1 then
					boxNeeded += 1
				elseif number == 2 then
					crateNeeded += 1
				elseif number == 3 then
					barrelNeeded += 1
				elseif number == 4 then
					metalNeeded += 1
				elseif number == 5 then
					boxNeeded += 1
				end

I am reaching the peak of my insanity, i tried using tables, but apparently they clone the variable and dosn’t add anything to them, any idea would be nice

1 Like

The only thing I could really think of is this

not tested for bugs

local needed = {
	{ name = "box", value = 0, indexes = {1, 5} },
	{ name = "crate", value = 0, indexes = {2} },
	{ name = "barrel", value = 0, indexes = {3} },
	{ name = "metal", value = 0, indexes = {4} }
}

function getNeededByIndex(index)
	for _, v in pairs(needed) do
		if (table.find(v.indexes, index)) then
			return v
		end
	end
	
	return nil
end

if game.ServerStorage.Day.Value <= 2 then
	number = math.random(0, 3)
elseif game.ServerStorage.Day.Value <= 5 and game.ServerStorage.Day.Value >= 2 then
	number = math.random(0, 5)
end

local neededItem = getNeededByIndex(number)
if (neededItem) then
	neededItem.value += 1
end

I made a lot of assumptions as to what your code intends to do, but you can combine all your variables into a dictionary (for the example code below, it’s called need).

local dayVal = game.ServerStorage:WaitForChild'Day'.Value;

local need = {
	barrel = 0;
	box = 0;
	crate = 0;
	metal = 0;
};

local order = {
	'box';
	'crate';
	'barrel';
	'metal';
	'box';
};

do --your scope
	if dayVal > 2 then return; end --inverted your conditional to save me some nesting
	
	local num = math.random(0, dayVal == 2 and 5 or 3);
	
	if not order[num] then return; end --escape for any value that doesnt correlate to an order

	need[order[num]] += 1; --the second conditional would only ever fire if day's value was exactly 2 due to the first, not sure what you were trying to do. also consider random:nextinteger here
end