Advertisement
SxScripting

Trinket Spawning System

Jan 30th, 2021
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. --Created By Soovier And Striker2783
  2.  
  3. local ItemsInGame = 0
  4. local Duration = 5
  5. local MaxItems = 8
  6. --Basic Variables
  7. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  8. local MoneyItems = ReplicatedStorage.MoneyItems
  9. local TrinketLocations = game.Workspace.TrinketLocations
  10.  
  11. local RS = game:GetService("ReplicatedStorage")
  12. --Debounce for spawning
  13. local Debs = false
  14.  
  15. local MoneyItems = RS.MoneyItems
  16.  
  17. --Table for the chance of spawning a trinket
  18. local TrinketTable = {
  19. [MoneyItems.Scroll] = 5; --5%
  20. [MoneyItems.Diamond] = 10; --10%
  21. [MoneyItems.Opal] = 35; --35%
  22. [MoneyItems.Goblet] = 50; --50%
  23. };
  24. --Total Weights
  25. local TrinketTotal = 0
  26. --Finding the total weights.
  27. for i,v in pairs(TrinketTable) do
  28. TrinketTotal = TrinketTotal + v
  29. end
  30. --Positions to spawn
  31. local AvailiblePos = {}
  32. --Makes the table above with the positions
  33. for i,v in pairs(TrinketLocations:GetChildren()) do
  34. AvailiblePos[#AvailiblePos + 1] = v
  35. end
  36. --Picks a random Trinket based on the weights
  37. local function SpinTrinket()
  38. local rand = math.random() * TrinketTotal
  39. local Add = 0
  40. for i, v in pairs(TrinketTable) do
  41. if rand > Add and rand <= v + Add then
  42. return i
  43. else
  44. Add = Add + v
  45. end
  46. end
  47. return SpinTrinket()
  48. end
  49. --Creates the Trinket
  50. function CreateNewItem()
  51. if ItemsInGame >= 8 or Debs then return end --Checks the amount of items and if it can spawn or not
  52. Debs = true
  53. local NewItem = SpinTrinket():Clone() --Clones a randomly picked Trinket
  54. local Pick = math.random(1,#AvailiblePos) --Gets the Index of the table in AvailiblePos
  55. local Pos = AvailiblePos[Pick] --Gets the Part that has the position
  56. table.remove(AvailiblePos, Pick) --Removes the Part that has the position from the AvailiblePos list
  57. local TrinketRay = workspace:Raycast(Pos.Position, Vector3.new(0,-180,0) * 30) --Puts a Trinket on the higher block
  58. if not TrinketRay then return end --Checks if it intersects anything
  59. NewItem.Position = TrinketRay.Position --Places the Trinket
  60. NewItem.Parent = workspace --Parents the Trinket
  61. ItemsInGame += 1 --Adds the amount of in game items
  62. NewItem.ClickDetector.MouseClick:Connect(function(plr) --Event that checks if the Trinket is clicked
  63. ItemsInGame -= 1 --Removes 1 from game items
  64. AvailiblePos[#AvailiblePos + 1] = Pos --Re adds the position to AvailiblePos
  65. NewItem:Destroy() --Destroys the Trinket
  66. plr.Silver.Value = plr.Silver.Value + 5 --Adds 5 silver to the player
  67. wait(Duration) --Waits Duration
  68. if ItemsInGame < MaxItems then --Checks to make sure there's enough items
  69. for i = 1, MaxItems - ItemsInGame do --Repeats the amount of times the items in game are off by max times
  70. if not CreateNewItem() then --Checks whether the creation of the trinket was successful
  71. wait(Duration) --Waits Duration
  72. end
  73. end
  74. end
  75. end)
  76. wait(Duration) --Waits Duration
  77. Debs = false
  78. return true
  79. end
  80.  
  81. while ItemsInGame < MaxItems do --Just for the start to get the max items
  82. CreateNewItem() --Creates New Trinkets
  83. wait() --Waits to not break the script.
  84. end
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement