Facebook
From Silly Crocodile, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 124
  1. local players = game:GetService("Players")
  2. local replicatedStorage = game:GetService("ReplicatedStorage")
  3. local serverStorage = game:GetService("ServerStorage")
  4. local animationSystem = replicatedStorage:WaitForChild("Modules"):WaitForChild("AnimationSystem")
  5. local attacks = script:WaitForChild("Attacks")
  6. local questSystem = replicatedStorage:WaitForChild("Modules"):WaitForChild("QuestSystem")
  7. local playSoundEffect = replicatedStorage:WaitForChild("Modules"):WaitForChild("PlaySoundEffect")
  8. local serverSettings = replicatedStorage:WaitForChild("ServerSettings")
  9. local module = {}
  10.  
  11. function GetRarityColor(itemName)
  12.         if replicatedStorage.Items:FindFirstChild(itemName) and replicatedStorage.Items[itemName]:FindFirstChild("Price") then
  13.                 local ItemPrice = replicatedStorage.Items[itemName].Price.Value
  14.                 if ItemPrice < 1000 then
  15.                         return Color3.fromRGB(255,255,255)
  16.                 elseif ItemPrice < 10000 then
  17.                         return Color3.fromRGB(255, 255, 127)
  18.                 elseif ItemPrice < 30000 then
  19.                         return Color3.fromRGB(85, 255, 127)
  20.                 elseif ItemPrice < 70000 then
  21.                         return Color3.fromRGB(255, 85, 85)
  22.                 elseif ItemPrice < 100000 then
  23.                         return Color3.fromRGB(255, 0, 255)
  24.                 else
  25.                         return Color3.fromRGB(0, 255, 255)
  26.                 end
  27.         else
  28.                 return Color3.fromRGB(255,255,255)
  29.         end
  30. end
  31.  
  32.  
  33. function module.SpawnEnemy(enemyName, position, folder, area)
  34.         if replicatedStorage.EnemyTypes[serverStorage.Enemies[enemyName].Stats.EnemyType.Value] then
  35.                 local waitToRespawn = Instance.new("Model", folder)
  36.                 local areaFromWait = Instance.new("StringValue")
  37.                 areaFromWait.Name = "AreaFrom"
  38.                 areaFromWait.Value = area.Name
  39.                 areaFromWait.Parent = waitToRespawn
  40.                 game:GetService("Debris"):AddItem(waitToRespawn, math.random(5,15))
  41.                 local enemy = serverStorage.Enemies[enemyName]:Clone()
  42.                 for _,stat in pairs(replicatedStorage.EnemyTypes[enemy.Stats.EnemyType.Value]:GetChildren()) do
  43.                         stat:Clone().Parent = enemy.Stats
  44.                 end
  45.                 enemy.EnemyHumanoid.Health = enemy.EnemyHumanoid.MaxHealth -- just in case someone forgot to set health
  46.                 local rankTag = Instance.new("NumberValue")
  47.                 rankTag.Value = enemy.Stats.Rank.Value
  48.                 rankTag.Name = "Rank"
  49.                 rankTag.Parent = enemy.Stats
  50.                 local areaFrom = Instance.new("StringValue")
  51.                 areaFrom.Name = "AreaFrom"
  52.                 areaFrom.Value = area.Name
  53.                 areaFrom.Parent = enemy
  54.                 local originalPosition = Instance.new("Vector3Value")
  55.                 originalPosition.Value = position
  56.                 originalPosition.Name = "OriginalPosition"
  57.                 originalPosition.Parent = enemy.Stats
  58.                 if not enemy.PrimaryPart then
  59.                         enemy.PrimaryPart = enemy:FindFirstChild("HumanoidRootPart") -- Everything with a humanoid must have a root part
  60.                 end
  61.                 enemy:SetPrimaryPartCFrame(CFrame.new(position) * CFrame.Angles(0, math.rad(math.random(-360, 360)), 0))
  62.                 enemy.Parent = folder
  63.                 local nametag = game:GetService("ServerScriptService").RPGSystem.Nametag:Clone()
  64.                 nametag.Parent = enemy.Head
  65.                 nametag.NameLabel.Text = enemy.Name --.. " [" .. enemy.Stats.Rank.Value .. "]"
  66.                 if enemy.Stats.Defense.Value > 0 and serverSettings.EnemiesShowDefense.Value == true then
  67.                         nametag.Defense.Visible = true
  68.                         nametag.Defense.Label.Text = enemy.Stats.Rank.Value
  69.                 end
  70.                 local newAnimationSystem = animationSystem:Clone()
  71.                 newAnimationSystem.Parent = enemy.EnemyHumanoid
  72.                 require(newAnimationSystem).PlayAnimation(enemy.Stats.IdleAnimation.Value.Name)
  73.                 enemy.EnemyHumanoid.Running:Connect(function(speed)
  74.                         if speed > 0.1 then
  75.                                 require(newAnimationSystem).PlayAnimation(enemy.Stats.WalkAnimation.Value.Name)
  76.                         else
  77.                                 require(newAnimationSystem).StopAnimation(enemy.Stats.WalkAnimation.Value.Name)
  78.                         end
  79.                 end)
  80.                 enemy.EnemyHumanoid.HealthChanged:Connect(function()
  81.                         local function Loot()
  82.                                 local alreadyGotLoot = {}
  83.                                 if enemy:FindFirstChild("LootTags") then
  84.                                         for _,tag in pairs(enemy.LootTags:GetChildren()) do
  85.                                                 if not table.find(alreadyGotLoot, tag.Name) then
  86.                                                         table.insert(alreadyGotLoot, tag.Name)
  87.                                                         local player = players:FindFirstChild(tag.Name)
  88.                                                         if player then
  89.                                                                 if enemy.Stats:FindFirstChild("Drops") then -- Loot drops
  90.                                                                         for _,itemDrop in pairs(enemy.Stats.Drops:GetChildren()) do
  91.                                                                                 local DropChance = math.random(1,1000)
  92.                                                                                 if player:FindFirstChild("Boosts") and player.Boosts:FindFirstChild("X2LOOT") and player.Boosts.X2LOOT.Value > 0 then
  93.                                                                                         DropChance /= 2
  94.                                                                                 end
  95.                                                                                 if DropChance <= itemDrop.DropChance.Value then
  96.                                                                                         if not itemDrop:FindFirstChild("DropType") or itemDrop.DropType.Value == "Item" then
  97.                                                                                                 if player.Inventory:FindFirstChild(itemDrop.Name) then
  98.                                                                                                         player.Inventory[itemDrop.Name].Value += 1
  99.                                                                                                         if replicatedStorage.ServerSettings.LootNotifications.Value == true then
  100.                                                                                                                 if player:FindFirstChild("Boosts") and player.Boosts:FindFirstChild("X2LOOT") and player.Boosts.X2LOOT.Value > 0 then
  101.                                                                                                                         replicatedStorage.ClientRemotes.Notification:FireClient(player, "Got " .. itemDrop.Name .. "! (2x Bonus)", Color3.fromRGB(255, 255, 0))
  102.                                                                                                                 else
  103.                                                                                                                         replicatedStorage.ClientRemotes.Notification:FireClient(player, "Got " .. itemDrop.Name .. "!", Color3.fromRGB(255, 255, 0))
  104.                                                                                                                 end
  105.                                                                                                         end
  106.                                                                                                 else
  107.                                                                                                         if #player.Inventory:GetChildren() < player.Stats.BagSlot.Value then
  108.                                                                                                                 if not player.StarterGear:FindFirstChild(itemDrop.Name) then
  109.                                                                                                                         replicatedStorage.Items[itemDrop.Name]:Clone().Parent = player.StarterGear
  110.                                                                                                                 end
  111.  
  112.                                                                                                                 if not player.Backpack:FindFirstChild(itemDrop.Name) then
  113.                                                                                                                         replicatedStorage.Items[itemDrop.Name]:Clone().Parent = player.Backpack
  114.                                                                                                                 end
  115.                                                                                                                 if replicatedStorage.ServerSettings.LootNotifications.Value == true then
  116.                                                                                                                         if player:FindFirstChild("Boosts") and player.Boosts:FindFirstChild("X2LOOT") and player.Boosts.X2LOOT.Value > 0 then
  117.                                                                                                                                 replicatedStorage.ClientRemotes.Notification:FireClient(player, "Got " .. itemDrop.Name .. "! (2x Bonus)", Color3.fromRGB(255, 255, 0))
  118.                                                                                                                         else
  119.                                                                                                                                 replicatedStorage.ClientRemotes.Notification:FireClient(player, "Got " .. itemDrop.Name .. "!", Color3.fromRGB(255, 255, 0))
  120.                                                                                                                         end
  121.                                                                                                                 end
  122.                                                                                                                 if not player.Inventory:FindFirstChild(itemDrop.Name) then
  123.                                                                                                                         local itemSaved = Instance.new("IntValue")
  124.                                                                                                                         itemSaved.Name = itemDrop.Name
  125.                                                                                                                         itemSaved.Parent = player.Inventory
  126.                                                                                                                         itemSaved.Value = 1
  127.                                                                                                                 end
  128.                                                                                                         else
  129.                                                                                                                 if replicatedStorage.ServerSettings.LootNotifications.Value == true then
  130.  
  131.                                                                                                                         if player:FindFirstChild("Boosts") and player.Boosts:FindFirstChild("X2LOOT") and player.Boosts.X2LOOT.Value > 0 then
  132.                                                                                                                                 replicatedStorage.ClientRemotes.Notification:FireClient(player, "Got " .. itemDrop.Name .. " but your inventory is full! (2x Bonus)", Color3.fromRGB(255, 0, 4))
  133.                                                                                                                         else
  134.                                                                                                                                 replicatedStorage.ClientRemotes.Notification:FireClient(player, "Got " .. itemDrop.Name .. " but your inventory is full!", Color3.fromRGB(255, 0, 4))
  135.                                                                                                                         end
  136.                                                                                                                 end
  137.                                                                                                                 local itemDropped = replicatedStorage.ItemsFolders:FindFirstChild(itemDrop.Name, true):Clone()
  138.                                                                                                                 itemDropped.CanCollide = false
  139.                                                                                                                 local proximityPront = script.Part.Attachment:Clone()
  140.                                                                                                                 proximityPront.Parent = itemDropped
  141.                                                                                                                 proximityPront.ProximityPrompt.ObjectText = itemDropped.Name
  142.                                                                                                                 proximityPront.ProximityPrompt.Enabled = true  
  143.                                                                                                                 itemDropped.CFrame = enemy.HumanoidRootPart.CFrame * CFrame.new(math.random(-10,10), 1,math.random(-10,10))
  144.                                                                                                                 local plasma = script.Plasma:Clone()
  145.                                                                                                                 local color = GetRarityColor(itemDropped.Name)
  146.                                                                                                                 plasma.Color = ColorSequence.new(color,color)
  147.                                                                                                                 plasma.Parent = itemDropped
  148.                                                                                                                 itemDropped.Parent = workspace.Loots
  149.                                                                                                                 game:GetService("Debris"):AddItem(itemDropped, 30)
  150.                                                                                                         end
  151.                                                                                                 end
  152.                                                                                                
  153.                                                                                         elseif itemDrop.DropType.Value == "Stat" then
  154.                                                                                                 player.Stats[itemDrop.Name].Value = player.Stats[itemDrop.Name].Value + itemDrop.Amount.Value
  155.                                                                                                 if replicatedStorage.ServerSettings.LootNotifications.Value == true then
  156.                                                                                                         if player:FindFirstChild("Boosts") and player.Boosts:FindFirstChild("X2LOOT") and player.Boosts.X2LOOT.Value > 0 then
  157.                                                                                                                 replicatedStorage.ClientRemotes.Notification:FireClient(player, "+" .. itemDrop.Amount.Value .. " " .. itemDrop.Name .. " (2x Bonus)", Color3.fromRGB(0, 255, 255))
  158.                                                                                                         else
  159.                                                                                                                 replicatedStorage.ClientRemotes.Notification:FireClient(player, "+" .. itemDrop.Amount.Value .. " " .. itemDrop.Name, Color3.fromRGB(0, 255, 255))
  160.                                                                                                         end
  161.                                                                                                 end
  162.                                                                                         else
  163.                                                                                                 warn('Item drop type named"' .. itemDrop.DropType.Value .. '" does not match any existing drop types; enemy: ' .. enemy.Name)
  164.                                                                                         end
  165.                                                                                 end
  166.                                                                         end
  167.                                                                 end
  168.                                                                 -- XP and Gold drops
  169.                                                                 player.Stats.XP.Value = player.Stats.XP.Value + enemy.Stats.XP.Value
  170.                                                                 if player:FindFirstChild("Boosts") and player.Boosts:FindFirstChild("X2XP") and player.Boosts.X2LOOT.Value > 0 then
  171.                                                                         player.Stats.XP.Value = player.Stats.XP.Value + enemy.Stats.XP.Value
  172.                                                                 end
  173.                                                                 player.Stats.Gold.Value = player.Stats.Gold.Value + enemy.Stats.Gold.Value
  174.                                                                 if player:FindFirstChild("Boosts") and player.Boosts:FindFirstChild("X2LOOT") and player.Boosts.X2LOOT.Value > 0 then
  175.                                                                         player.Stats.Gold.Value = player.Stats.Gold.Value + enemy.Stats.Gold.Value
  176.                                                                 end
  177.                                                                 -- Quest stuff
  178.                                                                 require(questSystem).PlayerDefeatedEnemy(player, enemy.Name)
  179.                                                                 -- codigo da moeda
  180.                                                                 --for i = 1,math.floor(enemy.Stats.Gold.Value/2) do wait()
  181.                                                                 --      local coins = game.ReplicatedStorage.Particles.Gold:Clone()
  182.                                                                 --      coins.Parent = workspace
  183.                                                                 --      game.Debris:AddItem(coins,20)
  184.                                                                 --      coins.Anchored = true
  185.                                                                 --      coins.CFrame = enemy:FindFirstChild('HumanoidRootPart').CFrame * CFrame.new(math.random(-6,6),math.random(-6,6),math.random(-6,6))
  186.                                                                 --      coins.r.Position = coins.CFrame.Position
  187.                                                                 --      coroutine.resume(coroutine.create(function()
  188.                                                                 --              coins.Anchored = false
  189.                                                                 --              wait(.25)
  190.                                                                 --              coins.r.Position = player.Character:FindFirstChild('HumanoidRootPart').CFrame.Position
  191.                                                                 --              for i = 1,10 do wait()
  192.                                                                 --                      coins.Transparency += 0.1
  193.                                                                 --                      coins.r.Position = player.Character:FindFirstChild('HumanoidRootPart').CFrame.Position
  194.                                                                 --              end
  195.                                                                 --              coins:Destroy()
  196.                                                                 --      end))
  197.                                                                 --end
  198.                                                         end
  199.                                                 end
  200.                                                 if players:FindFirstChild(tag.Name) and not players[tag.Name].Wiki.Monsters:FindFirstChild(enemy.Name) then
  201.                                                         local monster = Instance.new("IntValue", players:FindFirstChild(tag.Name).Wiki.Monsters)
  202.                                                         monster.Name = enemy.Name
  203.                                                         monster.Value = 1
  204.                                                 else
  205.                                                         players[tag.Name].Wiki.Monsters:FindFirstChild(enemy.Name).Value += 1
  206.                                                 end
  207.                                                 if players[tag.Name].Wiki.Monsters:FindFirstChild(enemy.Name).Value == serverStorage.Enemies[enemy.Name].Stats.Wiki.Value then
  208.                                                         replicatedStorage.ClientRemotes.Notification:FireClient(players[tag.Name], "You unlocked " .. enemy.Name .. " in Wiki!", Color3.fromRGB(0, 255, 0))
  209.                                                 end
  210.                                         end
  211.                                 end
  212.                         end
  213.                         if enemy.EnemyHumanoid.Health >= enemy.EnemyHumanoid.MaxHealth then
  214.                                 nametag.Bar.Visible = false
  215.                         else
  216.                                 nametag.Bar.Fill.Position = UDim2.new(1-(enemy.EnemyHumanoid.Health/enemy.EnemyHumanoid.MaxHealth), 0, 0, 0)
  217.                                 nametag.Bar.Visible = true
  218.                         end
  219.                         if enemy.EnemyHumanoid.Health == 0 and nametag.Parent then
  220.                                 Loot()
  221.                                 nametag:Destroy()
  222.                                 replicatedStorage.ClientRemotes.EnemyDied:FireAllClients(enemy)
  223.                                 game:GetService("Debris"):AddItem(enemy, 2)
  224.                         end
  225.                 end)
  226.         else
  227.                 warn("Tried to spawn " .. enemyName .. " but couldn't find a matching folder in EnemyTypes")
  228.         end
  229. end
  230.  
  231. function module.CalculateEnemyDamage(enemy, min, max)
  232.         return math.clamp(math.random(min, max) - enemy.Stats.Defense.Value, 1, math.huge)
  233. end
  234.  
  235. function module.CalculatePlayerDamage(player, min, max)
  236.         local defense = 0
  237.         if player and player:FindFirstChild("Stats") and player.Stats:FindFirstChild("CurrentArmor") and replicatedStorage.Items:FindFirstChild(player.Stats.CurrentArmor.Value) then
  238.                 defense = replicatedStorage.Items[player.Stats.CurrentArmor.Value].Defense.Value + (player.Attributes.Vitality.Value*2)
  239.         end
  240.         local minimo
  241.         if min < 10 then
  242.                 minimo = 1
  243.         else
  244.                 minimo = player.Attributes.Vitality.Value/1000*min*2
  245.         end
  246.         return math.clamp(math.random(min, max) - defense, math.floor(minimo), math.huge)
  247. end
  248.  
  249. function module.DamageEnemy(player, item, enemyHumanoid, damageMultiplier)
  250.         if enemyHumanoid.Health > 0 and not enemyHumanoid:FindFirstChild(player.Name) then
  251.                 local function TagEnemy(seconds)
  252.                         if not enemyHumanoid.Parent:FindFirstChild("LootTags") then
  253.                                 local lootTags = Instance.new("Folder")
  254.                                 lootTags.Name = "LootTags"
  255.                                 lootTags.Parent = enemyHumanoid.Parent
  256.                         end
  257.                         local tag = Instance.new("Model")
  258.                         tag.Name = player.Name
  259.                         tag.Parent = enemyHumanoid
  260.                         game:GetService("Debris"):AddItem(tag, seconds)
  261.                         local lootTag = Instance.new("Model")
  262.                         lootTag.Name = player.Name
  263.                         if not enemyHumanoid.Parent.LootTags:FindFirstChild(player.Name) then
  264.                                 lootTag.Parent = enemyHumanoid.Parent.LootTags
  265.                         end
  266.                         game:GetService("Debris"):AddItem(lootTag, 300)
  267.                 end
  268.                 local function LerpNumber(minimum, maximum, percentage)
  269.                     return (percentage * (maximum - minimum)) + minimum
  270.                 end
  271.                 TagEnemy(item.Cooldown.Value)
  272.                 if not damageMultiplier then
  273.                         damageMultiplier = 1
  274.                 end
  275.                 local plusDamage = 0
  276.                 if item.ItemType.Value == "Staff" then plusDamage = player.Attributes.Mythicality.Value end
  277.                 if item.ItemType.Value == "Sword" then plusDamage = player.Attributes.Power.Value end
  278.                 local minimum = item.MinimumDamage.Value
  279.                 local maximum = item.MaximumDamage.Value + plusDamage
  280.                 minimum = LerpNumber(minimum, maximum, player.Attributes.Power.Value / serverSettings.PowerCap.Value) -- The minumum increases with more power; max power is only maximum damage
  281.                 local damage = math.floor(module.CalculateEnemyDamage(enemyHumanoid.Parent, minimum, maximum) * damageMultiplier)
  282.                 enemyHumanoid.Health = enemyHumanoid.Health - damage
  283.                 replicatedStorage.ClientRemotes.DamageDone:FireClient(player, enemyHumanoid.Parent, damage)
  284.         end
  285. end
  286.  
  287. function module.FindClosestPlayer(enemy, useChaseRange) -- Returns closest player in the AttackRange or nil if none are found
  288.         if enemy.Name == "Model" then return end
  289.         local range = enemy.Stats.AttackRange.Value
  290.         if useChaseRange then
  291.                 range = enemy.Stats.ChaseRange.Value
  292.         end
  293.         local closest = nil
  294.         for _,player in pairs(players:GetChildren()) do
  295.                 if player.Character and player.Character:FindFirstChild("HumanoidRootPart") and player.Character:FindFirstChild("Humanoid") and enemy:FindFirstChild("HumanoidRootPart") and player.Character.Humanoid.Health > 0 then
  296.                         if not closest or (player.Character.HumanoidRootPart.Position - enemy.HumanoidRootPart.Position).magnitude < (closest.Character.HumanoidRootPart.Position - enemy.HumanoidRootPart.Position).magnitude then
  297.                                 if (player.Character.HumanoidRootPart.Position - enemy.HumanoidRootPart.Position).magnitude < range then
  298.                                         closest = player
  299.                                 end
  300.                         end
  301.                 end
  302.         end
  303.         return closest
  304. end
  305.  
  306. function module.FindPlayersInRadius(position, radius) -- Returns a table of players in the AttackRange or nil if none are found
  307.         local inRange = nil
  308.         for _,player in pairs(players:GetChildren()) do
  309.                 if player.Character and player.Character:FindFirstChild("HumanoidRootPart") and player.Character:FindFirstChild("Humanoid") and player.Character.Humanoid.Health > 0 then
  310.                         if (player.Character.HumanoidRootPart.Position - position).magnitude < radius then
  311.                                 if not inRange then
  312.                                         inRange = {}
  313.                                 end
  314.                                 table.insert(inRange, player)
  315.                         end
  316.                 end
  317.         end
  318.         return inRange
  319. end
  320.  
  321. function module.GetAllPlayerCharacters() -- Returns a table of all player characters, useful for whitelists
  322.         local characters = {}
  323.         for _,player in pairs(players:GetChildren()) do
  324.                 if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
  325.                         table.insert(characters, player.Character)
  326.                 end
  327.         end
  328.         return characters
  329. end
  330.  
  331. function module.Attack(enemy)
  332.         if enemy:FindFirstChild("EnemyHumanoid") and enemy.EnemyHumanoid.Health > 0 and enemy:FindFirstChild("HumanoidRootPart") and enemy:FindFirstChild("Stats") and not enemy:FindFirstChild("AttackCooldown") then
  333.                 local enemyType = enemy.Stats.EnemyType.Value
  334.                 if require(attacks)[enemyType] then
  335.                         local tag = Instance.new("Model") -- Does not let the enemy attack until the set AttackCooldown is over
  336.                         tag.Name = "AttackCooldown"
  337.                         tag.Parent = enemy
  338.                         require(attacks)[enemyType](enemy)
  339.                         wait(replicatedStorage.EnemyTypes[enemyType].AttackCooldown.Value)
  340.                         tag:Destroy()
  341.                 else
  342.                         warn(enemy.Name .. [[ tried to attack but the Attacks module didn't have an attack that matches the enemy type "]] .. enemyType .. [["]])
  343.                 end
  344.         end
  345. end
  346.  
  347. function module.Follow(enemy, targetPart)
  348.         local chaseRange = enemy.Stats.ChaseRange.Value -- How many studs away a player must be before the enemy decides to start chasing
  349.         local closestRange = enemy.Stats.ClosestRange.Value -- The minumum distance the enemy can be from the player
  350.         local attackRange = enemy.Stats.AttackRange.Value -- How many studs away the enemy will try to attack a player
  351.         if (targetPart.Position - enemy.HumanoidRootPart.Position).magnitude <= chaseRange then
  352.                 local pointToSeek = targetPart.Position+(CFrame.new(targetPart.Position, enemy.HumanoidRootPart.Position).LookVector*closestRange)
  353.                 enemy.EnemyHumanoid:MoveTo(pointToSeek)
  354.                 if (targetPart.Position - enemy.HumanoidRootPart.Position).magnitude <= attackRange then
  355.                         module.Attack(enemy)
  356.                 end
  357.         elseif (targetPart.Position - enemy.Stats.OriginalPosition.Value).magnitude > 10 then
  358.                 enemy.EnemyHumanoid:MoveTo(enemy.Stats.OriginalPosition.Value)
  359.         end
  360. end
  361.  
  362. return module