Advertisement
HowToRoblox

HatchHandler

Aug 7th, 2020 (edited)
3,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.05 KB | None | 0 0
  1. local incubator = script.Parent
  2.  
  3. local buyButton = incubator.BuyButton
  4. local clickDetector = buyButton.ClickDetector
  5. local buyTextLabel = buyButton.BuyGui.BuyTextLabel
  6.  
  7. local egg = incubator.Egg
  8.  
  9. local re = incubator.HatchRE
  10.  
  11.  
  12. local petsFolder = game.ReplicatedStorage.HatchablePets
  13.  
  14.  
  15. local plrsHatching = {}
  16.  
  17.  
  18. local price = 1000
  19.  
  20.  
  21. local rarities =
  22. {
  23.     Common = 40,
  24.     Uncommon = 30,
  25.     Rare = 15,
  26.     Epic = 10,
  27.     Legendary = 5,
  28. }
  29. local rarityColours =
  30. {
  31.     Common = Color3.fromRGB(162, 169, 181),
  32.     Uncommon = Color3.fromRGB(15, 185, 40),
  33.     Rare = Color3.fromRGB(43, 85, 223),
  34.     Epic = Color3.fromRGB(150, 24, 223),
  35.     Legendary = Color3.fromRGB(223, 141, 25),
  36. }
  37.  
  38.  
  39. game.Players.PlayerAdded:Connect(function(plr)
  40.    
  41.     local ls = Instance.new("Folder")
  42.     ls.Name = "leaderstats"
  43.     ls.Parent = plr
  44.    
  45.     local cash = Instance.new("IntValue")
  46.     cash.Name = "Cash"
  47.     cash.Value = 1000
  48.     cash.Parent = ls
  49. end)
  50.  
  51.  
  52. clickDetector.MouseClick:Connect(function(plrClicked)
  53.    
  54.     if plrsHatching[plrClicked] then return end
  55.     if plrClicked.leaderstats.Cash.Value < price then return end
  56.    
  57.     plrsHatching[plrClicked] = true
  58.    
  59.     plrClicked.leaderstats.Cash.Value = plrClicked.leaderstats.Cash.Value - price
  60.    
  61.    
  62.     local petsToPick = {}
  63.    
  64.     for i, hatchablePet in pairs(petsFolder:GetChildren()) do
  65.        
  66.         local rarity = hatchablePet.Rarity.Value
  67.        
  68.         for i = 1, rarities[rarity] do
  69.            
  70.             table.insert(petsToPick, hatchablePet)
  71.         end
  72.     end
  73.    
  74.     local chosenPet = petsToPick[math.random(1, #petsToPick)]
  75.    
  76.    
  77.     re:FireClient(plrClicked, chosenPet, rarityColours[chosenPet.Rarity.Value])
  78.    
  79.     re.OnServerEvent:Wait()
  80.    
  81.    
  82.     plrsHatching[plrClicked] = false
  83.    
  84.    
  85.     local char = plrClicked.Character
  86.     local hrp = char.HumanoidRootPart
  87.    
  88.     local petClone = chosenPet:Clone()
  89.    
  90.     petClone.CFrame = hrp.CFrame - (hrp.CFrame.RightVector * 5)
  91.    
  92.     local wConstraint = Instance.new("WeldConstraint")
  93.     wConstraint.Part0 = hrp
  94.     wConstraint.Part1 = petClone
  95.     wConstraint.Parent = petClone
  96.    
  97.     petClone.Anchored = false
  98.     petClone.CanCollide = false
  99.    
  100.     petClone.Parent = hrp
  101. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement