How can i make my numbers random and unique for each rarity

local Items = {}

local itemTable = {

	["C_BasicWand"] = {Name = "Basic Wand", Rarity = "Common", Physical = Random.new(os.time()):NextInteger(1,3), Spell = Random.new(os.time()):NextInteger(3,8), Value = 1, Req = 1, Upgrades = Random.new(os.time()):NextInteger(3,10), CurrentUpgrades = 0},
	["UC_BasicWand"] = {Name = "Basic Wand", Rarity = "Uncommon", Physical = Random.new(os.time()):NextInteger(1,4), Spell = Random.new(os.time()):NextInteger(5,11), Value = 1, Req = 1, Upgrades = Random.new(os.time()):NextInteger(6,15), CurrentUpgrades = 0},
	["R_BasicWand"] = {Name = "Basic Wand", Rarity = "Rare", Physical = Random.new(os.time()):NextInteger(2,6), Spell = Random.new(os.time()):NextInteger(9,16), Value = 1, Req = 1, Upgrades = Random.new(os.time()):NextInteger(8,20), CurrentUpgrades = 0},
	["E_BasicWand"] = {Name = "Basic Wand", Rarity = "Epic", Physical = Random.new(os.time()):NextInteger(2,8), Spell = Random.new(os.time()):NextInteger(15,26), Value = 1, Req = 1, Upgrades = Random.new(os.time()):NextInteger(14,25), CurrentUpgrades = 0},
	["L_BasicWand"] = {Name = "Basic Wand", Rarity = "Legendary", Physical = Random.new(os.time()):NextInteger(3,10), Spell = Random.new(os.time()):NextInteger(24,50), Value = 1, Req = 1, Upgrades = Random.new(os.time()):NextInteger(10,40), CurrentUpgrades = 0},


}

local lootTable = {

	{Name = itemTable.C_BasicWand, Chance = 55}, 
	{Name = itemTable.UC_BasicWand, Chance = 40}, 
	{Name = itemTable.R_BasicWand, Chance = 25}, 
	{Name = itemTable.E_BasicWand, Chance = 15}, 
	{Name = itemTable.L_BasicWand, Chance = 1}, 
}

local function returnSumOfWeight(lootTable)
	local sum = 0
	for _, item in ipairs(lootTable) do
		sum = sum + item.Chance
	end
	return sum
end

local function getRandomItem(lootTable)
	local randomNumber = math.random(returnSumOfWeight(lootTable))

	for _, item in ipairs(lootTable) do
		if  randomNumber <= item.Chance then
			return item.Name
		else
			randomNumber = randomNumber - item.Chance
		end
	end
end

function Items.PickItem()

	local item = getRandomItem(lootTable)
	
	
	return item

end

return Items

in this script you are able to get a random item from the itemTable and it works as intended but I want all of the stats of each item to be unique and random, however when i test the game I get the items and they have the same stats for each rarity example: rare item will have 5 physical and 3 spell, if i get another rare item it will also have 5 physical and 3 spell, i do NOT want this i want each item to have different stats how can i alter my script to make this happen? like right now it sets a random stat and then its that stat until i do another playtest i want it to be random every single time i get a new item

This is happening because you randomize the stats once during the table declaration. Instead, you should randomize the stats every time you pick an item:

--Wraps the Random.new call to a simple function to make code more readable
function rand(a: number, b: number): number
	return Random.new(os.time()):NextInteger(a, b)
end

--Basically clones a dictionary/array in a recursive way
function deepCopy(original: {[number|string]: any}): {[number|string]: any}
	local copy = {}
	for k, v in pairs(original) do
		if type(v) == "table" then v = deepCopy(v) end
		copy[k] = v
	end
	return copy
end

--Define each item as the borders of randomness instead of picking the value
--Basically define the a's and b's:
local itemTable = {
	["C_BasicWand"] = {Name = "Basic Wand", Rarity = "Common", Physical = {1, 3}, Spell = {3, 8}, Value = 1, Req = 1, Upgrades = {3, 10}, CurrentUpgrades = 0},
	["UC_BasicWand"] = {Name = "Basic Wand", Rarity = "Uncommon", Physical = {1, 4}, Spell = {5, 11}, Value = 1, Req = 1, Upgrades = {6, 15}, CurrentUpgrades = 0},
	["R_BasicWand"] = {Name = "Basic Wand", Rarity = "Rare", Physical = {2, 6}, Spell = {9, 16}, Value = 1, Req = 1, Upgrades = {8, 20}, CurrentUpgrades = 0},
	["E_BasicWand"] = {Name = "Basic Wand", Rarity = "Epic", Physical = {2, 8}, Spell = {15, 26}, Value = 1, Req = 1, Upgrades = {14, 25}, CurrentUpgrades = 0},
	["L_BasicWand"] = {Name = "Basic Wand", Rarity = "Legendary", Physical = {3, 10}, Spell = {24, 50}, Value = 1, Req = 1, Upgrades = {10, 40}, CurrentUpgrades = 0},
}

--Now when you pick every item:
function Items.PickItem()
	--We copy the item so no damage is done to the original item
	local item = deepCopy(getRandomItem(lootTable))
	--We now change the values so they change for this specific copy of the item
	item.Physical = rand(item.Physical[1], item.Physical[2])
	item.Spell = rand(item.Spell[1], item.Spell[2])
	item.Upgrades = rand(item.Upgrades[1], item.Upgrades[2])
	--Then we're done and return the configured item
	return item
end

Keep in mind this is only the code that needs editing, make sure to include the other parts of your code as well.

thank you so much, did not think it would need that much to fix it

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