Hello.
I want to create an item spawning system that increases per spawn rarity.
I have a base idea of how to do this, but I am not entirely sure.
Any random item can spawn at any spawner except for mythicals. Mythicals will only spawn if the spawn’s rarity is Rare+.
This is what I’ve done so far.
while true do
RunService.Heartbeat:Wait()
local children = workspace.TrinketSpawns:GetChildren()
for i = 1, #children do
local child = children[i]
local rarityType = child.Rarity.Value
local objVal = child.Trinket
local lastSpawned = child.LastSpawned
-- Quick checks
if objVal.Value then
return
end
if not rarities[rarityType] then
return
end
if tick() - lastSpawned.Value < globalCooldown and lastSpawned.Value > 0 then
return
end
-- We're in
print("boom")
lastSpawned.Value = tick()
local randomTrinket = rarities:GenerateRandomTrinket(rarityType)
local clonedTrinket = ServerStorage.SpawnableItems:FindFirstChild(randomTrinket.Name):Clone()
clonedTrinket.CFrame = child.CFrame
clonedTrinket.Parent = workspace.Trinkets
objVal.Value = clonedTrinket
end
end
Here are my current rarity types with the items:
function module:GetTrinketRarityTypes()
local rarityTypes = {
-----------------------------------
{
Rarity = {
Name = "Common",
Chance = .45,
},
Items = {
{Name = "Coin"}
},
};
-----------------------------------
{
Rarity = {
Name = "Uncommon",
Chance = .32
};
Items = {
{Name = "Silver Goblet"}
},
},
-----------------------------------
{
Rarity = {
Name = "Rare",
Chance = .145
};
Items = {
{Name = "Ruby"},
{Name = "Emerald"},
{Name = "Sapphire"},
},
};
-----------------------------------
{
Rarity = {
Name = "Legendary",
Chance = .08
};
Items = {
{Name = "Diamond"},
},
};
-----------------------------------
{
Rarity = {
Name = "Mythical",
Chance = .005
};
Items = {
{Name = "TNT"},
}
}
-----------------------------------
};
-- Just making a group index so I can use that later
for groupIndex, group in pairs(rarityTypes) do
for i, item in pairs(group.Items) do
item.GroupIndex = groupIndex
end
end
return rarityTypes
end
I haven’t done much here, but, this is what I’m trying to do in the function.
function module:GenerateRandomTrinket(spawnerRarity)
local modifiedItemTypes = module:GetTrinketRarityTypes()
-- Determine rarity
-- Return randomized trinket depending on spawnerRarity
return
end
This is all the setup for the spawning. I don’t know how to progress from here, so that’s why I’m asking for help.
Thank you.