local module = {}
local ServerStorage = game.ServerStorage
local variantes = require(script.Parent:WaitForChild("Variantes"))
local Monsters = ServerStorage:WaitForChild("Monsters")
local Bosses = ServerStorage:WaitForChild("Bosses")
local MessagingService = game:GetService("MessagingService")
MessagingService:SubscribeAsync("RarityKill", function(message)
game.ReplicatedStorage.Remotes.SystemChat:FireAllClients(message.Data.DisplayName, message.Data.Creature, message.Data.Rarity)
end)
local function calcTotals(categoria1, categoria2)
local total1 = 0
local total2 = 0
for i,bagulha in pairs(categoria1) do
total1=total1+bagulha.weight
end
for i,bagulha in pairs(categoria2) do
total2=total2+bagulha.weight
end
print("DEBUG: total1 =", total1, "total2 =", total2) -- Add this
return total1, total2
end
local function chanceEmTantos(weight, totalWeight, categoriaChance)
--categoriaChance 1 se categoria1, 5 de categoria2
print(totalWeight, weight, categoriaChance)
return totalWeight / weight * categoriaChance
end
local function chance1in(var1Data , var1Cat, var2Data , var2Cat, cat1, cat2)
local total1, total2 = calcTotals(cat1, cat2)
local function singleChance(vData, catRef)
local mult = (catRef == cat2) and 5 or 1
local total = (catRef == cat2) and total2 or total1
return chanceEmTantos(vData.weight, total, mult)
end
if not var2Data then
return singleChance(var1Data, var1Cat)
end
return math.floor(
singleChance(var1Data, var1Cat) *
singleChance(var2Data, var2Cat) + 0.5)
end
function module.GetRandomMonster(weightTable)
-- calcula o peso total
local totalWeight = 0
for name, data in pairs(weightTable) do
totalWeight = totalWeight + (data.weight or 0)
end
if totalWeight <= 0 then
warn("GetRandomMonster: totalWeight inválido (" .. tostring(totalWeight) .. ")")
return nil
end
-- obtém um número aleatório entre 1 e totalWeight
local pick = math.random(1, totalWeight)
-- percorre até que o valor acumulado alcance pick
local accumulated = 0
for name, data in pairs(weightTable) do
accumulated = accumulated + data.weight
if pick <= accumulated then
return name
end
end
-- fallback (não deveria acontecer)
return nil
end
module.ChanceByName = function(monsterName, variantName1, variantName2)
-- pull the pools (same call you already do elsewhere)
local pools = variantes.Variantes()
local cat1,cat2 = pools.Category1, pools.Category2
-- helper: find the data table + its category given a variant name
local function findVariant(name)
if not name then return nil,nil end -- caller passed nil
for _,cat in pairs({cat1,cat2}) do
if cat[name] then return cat[name], cat end
end
return nil,nil -- name not found
end
local v1, catV1 = findVariant(variantName1)
local v2, catV2 = findVariant(variantName2)
if not v1 and not v2 then -- none found? bail
warn("ChanceByName: no valid variants for "..monsterName)
return nil
end
-- *** reuse the generic function we wrote earlier ***
return chance1in(v1 , catV1,
v2 , catV2,
cat1, cat2) -- real totals
end
local function pick(pool) -- raridade?
local total, r = 0, math.random()
for _,def in pairs(pool) do total += def.weight or 0 end
r *= total
for name,def in pairs(pool) do
r -= def.weight
if r <= 0 then return name end
end
end
module.Varia = function(npc, monsterName, variant1, variant2, miniatura)
local randomvariant1 = nil
local randomvariant1Categoria = nil
local randomvariant2 = nil
local randomvariant2Categoria = nil
local times = 0
print(variant1)
print(variant2)
if variant1 == nil and variant2 == nil and miniatura == nil then
local todasCategorias = variantes.Variantes(monsterName)
local cat1 = todasCategorias.Category1
local cat2 = todasCategorias.Category2
local alreadyCategory2 = false
local chosen = cat1
if math.random(1,5) == 1 then
times = 1
if math.random(1,10) == 1 then
times = 2
end
end
for i = 1, times do
if math.random(1,5) == 1 and chosen ~= cat2 then
chosen = cat2
else
chosen = cat1
end
local todasVariantes = chosen
if todasVariantes then
local variantName = pick(todasVariantes)
local variantScript = todasVariantes[variantName].variante
if variantScript then
local scrip = script.Parent.VariantesScripts:FindFirstChild(variantScript)
if not scrip then
warn("nao tem isso no variantesscript "..variantScript)
return
end
require(scrip).Variar(npc)
end
if randomvariant1 == nil then
randomvariant1 = todasVariantes[variantName]
randomvariant1Categoria = chosen
else
randomvariant2 = todasVariantes[variantName]
randomvariant2Categoria = chosen
end
end
end
if randomvariant1 ~= nil then
local chance = chance1in(randomvariant1, randomvariant1Categoria,nil, nil, cat1, cat2)
print("1 em "..chance)
return chance
elseif randomvariant2 ~= nil then
local chance = chance1in(randomvariant2, randomvariant2Categoria,nil, nil, cat1, cat2)
print("1 em "..chance)
return chance
end
else
--CUSTOM VARIANTE
print("custom")
local todasCategorias = variantes.Variantes(monsterName)
local cat1 = todasCategorias.Category1
local cat2 = todasCategorias.Category2
local VarianteTrue1 = nil
local VarianteTrue2 = nil
local VarianteTrue1Cat = nil
local VarianteTrue2Cat = nil
local function VARIANTE(variantThis)
-- Use the existing cat1, cat2 instead of creating new ones
for _,cat in pairs({cat1, cat2}) do -- Changed this line
for vName,vData in pairs(cat) do
if vName == variantThis then
-- aplica script
local scriptName = vData.variante
if scriptName then
require(script.Parent.VariantesScripts[scriptName]).Variar(npc)
end
-- salva
if not VarianteTrue1 then
VarianteTrue1, VarianteTrue1Cat = vData, cat
else
VarianteTrue2, VarianteTrue2Cat = vData, cat
end
return
end
end
end
end
if variant1 then
VARIANTE(variant1)
end
if variant2 then
VARIANTE(variant2)
end
if VarianteTrue1 and VarianteTrue2 then
local chance = chance1in(VarianteTrue1 , VarianteTrue1Cat, VarianteTrue2 , VarianteTrue2Cat, cat1, cat2)
print("1 em "..chance)
return chance
elseif VarianteTrue1 then
local chance = chance1in(VarianteTrue1 , VarianteTrue1Cat, nil , nil, cat1, cat2)
print("DEBUG: VarianteTrue1Cat == cat1:", VarianteTrue1Cat == cat1)
print("DEBUG: VarianteTrue1Cat == cat2:", VarianteTrue1Cat == cat2)print("1 em "..chance)
return chance
elseif VarianteTrue2 then
local chance = chance1in(VarianteTrue2 , VarianteTrue2Cat, nil , nil, cat1, cat2)
print("1 em "..chance)
return chance
end
end
end
module.Summon = function(monsterName, position, variant1, variant2)
local npc = nil
task.spawn(function()
npc = Monsters:FindFirstChild(monsterName)
print("que")
if not npc then
warn("monstro n existe veir "..monsterName)
npc = Bosses:FindFirstChild(monsterName)
if not npc then
return
end
end
npc = npc:Clone()
print("que")
local brain = npc:FindFirstChild("Celebro")
if not brain then
return
end
local chance = module.Varia(npc, monsterName, variant1, variant2)
print("que")
local variant1 = npc:GetAttribute("Variant1") or ""
local variant2 = npc:GetAttribute("Variant2") or ""
local name = npc.Name
npc:SetAttribute("Type", name)
print("que")
local function nameThingAndPaint(npc, name, variant1, variant2)
if npc.Parent ~= nil and npc.Parent.Name == "Handle" then
npc.Parent.Parent:SetAttribute("Variant1",variant1)
npc.Parent.Parent:SetAttribute("Variant2",variant2)
if chance then
npc.Parent.Parent:SetAttribute("Rarity",chance)
end
end
if variant1 == nil then variant1 = "" end
if variant2 == nil then variant2 = "" end
variant1 = variant1:sub(1,1):upper() .. variant1:sub(2) or ""
variant2 = variant2:sub(1,1):upper() .. variant2:sub(2) or ""
local name = npc.Name
name = variant1.." "..variant2.." "..name
name = name:gsub("%s+", " "):gsub("^%s+", ""):gsub("%s+$", "") --removes double spaces
npc.Name = name
if npc.Parent ~= nil and npc.Parent.Name == "Handle" then
npc.Parent.Parent.Name = name
end
if variant1 ~= "" or variant2 ~= "" then
print("NAME")
local nameText = npc:FindFirstChild("MonsterName")
if nameText ~= nil then
local function strokeColor(thing)
for i,v in pairs(nameText.Variety:GetChildren()) do
if v:FindFirstChild("UIStroke") then
v.UIStroke.Color = thing
end
end
end
local chanceLouca = math.floor(chance+0.5)
nameText.Normal.Visible = false
nameText.Variety.Visible = true
nameText.Variety.MonsterName.Text = name
nameText.Variety.Rarity.Text = "1 in "..chanceLouca
if chanceLouca > 1 and chance < 50 then--Common
strokeColor(Color3.fromRGB(17, 23, 80))
nameText.Variety.Title.Text = "Common"
nameText.Variety.Title.TextColor3 = Color3.fromRGB(0, 174, 255)
elseif chanceLouca > 50 and chanceLouca < 150 then--Uncommon
strokeColor(Color3.fromRGB(23, 44, 0))
nameText.Variety.Title.Text = "Uncommon"
nameText.Variety.Title.TextColor3 = Color3.fromRGB(18, 255, 1)
elseif chanceLouca > 150 and chanceLouca < 400 then--Rare
strokeColor(Color3.fromRGB(0, 0, 0))
nameText.Variety.Title.Text = "Rare"
nameText.Variety.Title.TextColor3 = Color3.fromRGB(255, 0, 0)
elseif chanceLouca > 400 and chanceLouca < 2000 then--Epic
strokeColor(Color3.fromRGB(30, 0, 255))
nameText.Variety.Title.Text = "Epic"
nameText.Variety.Title.TextColor3 = Color3.fromRGB(183, 0, 255)
elseif chanceLouca > 2000 and chanceLouca < 8000 then--Legendary
strokeColor(Color3.fromRGB(255, 119, 0))
nameText.Variety.Title.Text = "Legendary"
nameText.Variety.Title.TextColor3 = Color3.fromRGB(251, 255, 0)
elseif chanceLouca > 8000 then--Cosmic
strokeColor(Color3.fromRGB(0, 0, 0))
nameText.Variety.Title.Text = "Cosmic"
nameText.Variety.Title.TextColor3 = Color3.fromRGB(255, 255, 255)
for i,v in pairs(nameText.Variety:GetChildren()) do
local cosmic = script.CosmicGradient:Clone()
cosmic.Parent = v
end
end
end
end
end
print(variant1,variant2)
nameThingAndPaint(npc,npc.Name,variant1,variant2)
print("Do")
npc:MoveTo(position)
npc.Parent = workspace.Enemies
npc:MoveTo(position)
if chance then
npc:SetAttribute("Rarity",math.floor(chance+0.5))
end
print("que")
task.spawn(function()
local humanoid = npc:WaitForChild("Humanoid")
if not humanoid then
return
end
humanoid.Died:Connect(function()
local value = npc:FindFirstChild("MostDamage")
for i,v in pairs(value:GetChildren()) do
print(" player")
local name = v.Name
local plr = game.Players:FindFirstChild(name)
if plr == nil then
return
end
local backpack = plr:FindFirstChild("Backpack")
if not backpack then
return
end
print(" backpack")
local variant1 = npc:GetAttribute("Variant1")
local variant2 = npc:GetAttribute("Variant2")
local miniature = require(game.ServerStorage.Modules.Misccc).giveMiniature(plr, variant1, variant2, npc:GetAttribute("Type"))
if miniature == nil then
warn("no miniature")
return
end
local handle = miniature:FindFirstChild("Handle")
if handle == nil then
warn("no miniature handle")
return
end
local model = handle:FindFirstChildOfClass("Model")
if model == nil then
warn("no miniature model")
return
end
print("MINIATURE NOW")
print(model, model.Name, variant1, variant2)
local rarity = npc:GetAttribute("Rarity")
if rarity and rarity > 8000 then
warn("?")
MessagingService:PublishAsync("RarityKill", {DisplayName=plr.DisplayName,Creature=npc.Name,Rarity=rarity})
end
nameThingAndPaint(model,model.Name,variant1,variant2)
end
if npc:GetAttribute("Boss") == nil then
local parts = npc:GetChildren()
for i = 1, #parts do
if parts[i]:IsA("BasePart") then
game.TweenService:Create(parts[i],TweenInfo.new(1),{Transparency = 1,Size = Vector3.new(0,0,0)}):Play()
end
game.Debris:AddItem(parts[i],2)
end
game.Debris:AddItem(npc,1)
end
end)
require(brain).Start()
end)
end)
return npc
end
return module