I’m trying to implement a rarity system in my game, however, I’ve run through an issue that I can’t seem to figure out…
The problematic script:
function Roll()
local CurrentBestRarity = "Rock"
for RarityTier, Chance in OreArray do
local NewRoll = Rand:NextNumber()
if NewRoll < Chance then
if OreArray[CurrentBestRarity] > Chance then
CurrentBestRarity = RarityTier
end
end
end
return CurrentBestRarity
end
local spawnBricks = game.Workspace.Spawns:GetChildren()
local InitializeOre = 6
local function OreSpawn()
local OresToSpawn = {}
for i = 0, InitializeOre, 1 do
task.spawn(function()
local result = Roll()
table.insert(OresToSpawn, result)
end)
end
for Index, Ore in OresToSpawn do
local Index : number = math.random(#spawnBricks)
local Chosen : Part = spawnBricks[Index]
local FoundLocation = Spawns:FindFirstChild(Chosen.Name)
if Chosen.Occupied.Value == true then
repeat
Index = math.random(#spawnBricks)
Chosen = spawnBricks[Index]
FoundLocation = Spawns:FindFirstChild(Chosen.Name)
until Chosen.Occupied.Value == false
end
if Ore == "Rock" then
local rockClone = Ores[1]:Clone()
rockClone.Parent = FoundLocation
rockClone:PivotTo(FoundLocation.CFrame)
rockClone.Parent.Occupied.Value = true
elseif Ore == "IronOre" then
local ironClone = Ores[2]:Clone()
ironClone.Parent = FoundLocation
ironClone:PivotTo(FoundLocation.CFrame)
ironClone.Parent.Occupied.Value = true
elseif Ore == "GoldOre" then
local goldClone = Ores[3]
goldClone.Parent = FoundLocation
goldClone:PivotTo(FoundLocation.CFrame)
goldClone.Parent.Occupied.Value = true
elseif Ore == "AzureOre" then
local azureClone = Ores[4]:Clone()
azureClone.Parent = FoundLocation
azureClone:PivotTo(FoundLocation.CFrame)
azureClone.Parent.Occupied.Value = true
end
end
end
Players.PlayerAdded:Connect(OreSpawn)
The error: