Advertisement
PeaPattern

Tycoon

Mar 21st, 2025
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1. local DatastoreService = game:GetService("DataStoreService")
  2. local RStorage = game:GetService("ReplicatedStorage")
  3. local Players = game:GetService("Players")
  4.  
  5. local Datastore = DatastoreService:GetDataStore("PlayersData")
  6.  
  7. local Tycoon = script.Parent
  8.  
  9. local Claim = Tycoon.Claim
  10. local Head = Claim.Head
  11. local Humanoid = Claim.Humanoid
  12.  
  13. local isClaimed = false
  14. local Owner = nil
  15.  
  16. local function PlayerTouch(Obj, Callback)
  17.     Obj.Touched:Connect(function(Hit)
  18.         if not Hit or not Hit.Parent or not Hit.Parent:FindFirstChildOfClass("Humanoid") then return end
  19.  
  20.         local Target = Players:GetPlayerFromCharacter(Hit.Parent)
  21.         if not Target then return end
  22.  
  23.         Callback(Target)
  24.     end)
  25. end
  26.  
  27. local function PlayerLeft(Target, Callback)
  28.     local Connection
  29.     Connection = Players.PlayerRemoving:Connect(function(Player)
  30.         if Player ~= Target then return end
  31.  
  32.         Callback()
  33.         Connection:Disconnect()
  34.     end)
  35. end
  36.  
  37. local Storage = Instance.new("Folder")
  38. Storage.Parent = RStorage
  39. Storage.Name = "TycoonStorage"
  40.  
  41. local Ores = Instance.new("Folder")
  42. Ores.Parent = workspace
  43. Ores.Name = "Ores"
  44.  
  45. local function OwnsButton(Player, ButtonName)
  46.     local Success, Owns = pcall(function()
  47.         return Datastore:GetAsync(Player.UserId, ButtonName)
  48.     end)
  49.  
  50.     if Success and Owns then
  51.         return Owns
  52.     end
  53. end
  54.  
  55. local Buttons = {}
  56. local ButtonCount = 0
  57.  
  58. local function OperateButton(Price, Button, Model, Callback)
  59.     if #Buttons ~= 0 then
  60.         Button.Parent.Parent = Storage
  61.     end
  62.  
  63.     ButtonCount += 1
  64.     local Next = ButtonCount + 1
  65.     Buttons[ButtonCount] = {
  66.         Button.Parent,
  67.         Model,
  68.         ButtonCount,
  69.         Callback,
  70.     }
  71.  
  72.     Model.Parent = Storage
  73.     PlayerTouch(Button, function(Target)
  74.         if not isClaimed or Owner ~= Target then return end
  75.         if not Target or Target.leaderstats.Money.Value < Price then return end
  76.         Button.Parent.Parent = Storage
  77.         Model.Parent = Tycoon
  78.         Owner.leaderstats.Money.Value -= Price
  79.         Callback()
  80.  
  81.         local Found = Buttons[Next][1]
  82.         if Found then
  83.             Found.Parent = Tycoon
  84.         end
  85.     end)
  86. end
  87.  
  88. local function CreateOre(Price, Origin)
  89.     local Ore = Instance.new("Part")
  90.     Ore.Parent = Ores
  91.     Ore.Size = Vector3.new(1, 1, 1)
  92.     Ore.CFrame = Origin.CFrame
  93.     Ore:SetAttribute("Price", Price)
  94. end
  95.  
  96. local DropperConveyor = Tycoon.DropperConveyor
  97. OperateButton(0, Tycoon.Start.Head, DropperConveyor, function()
  98.     DropperConveyor.Conveyor.Velocity = Vector3.new(0, 0, 10)
  99.  
  100.     DropperConveyor.Furnace.Touched:Connect(function(Ore)
  101.         if not Ore or Ore.Parent ~= Ores then return end
  102.  
  103.         local Price = Ore:GetAttribute("Price")
  104.         Owner.leaderstats.Money.Value += Price
  105.  
  106.         Ore:Destroy()
  107.     end)
  108.  
  109.     task.spawn(function()
  110.         local DropPart = Tycoon.DropperConveyor.DropPart
  111.         while task.wait(2) do
  112.             if not isClaimed then break end
  113.             CreateOre(1, DropPart)
  114.         end
  115.     end)
  116. end)
  117.  
  118. OperateButton(15, Tycoon.UpgraderButton.Head, Tycoon.Upgrader, function()
  119.     local Connection = Tycoon.Upgrader.UpgradePart.Touched:Connect(function(Ore)
  120.         if not Ore or Ore.Parent ~= Ores then return end
  121.         if Ore:GetAttribute("Upgraded") then return end
  122.  
  123.         Ore:SetAttribute("Upgraded", true)
  124.         Ore:SetAttribute("Price", Ore:GetAttribute("Price") * 2)
  125.     end)
  126.     repeat task.wait() until not isClaimed
  127.     Connection:Disconnect()
  128. end)
  129.  
  130. local Old = {}
  131.  
  132. for _, Obj in Tycoon:GetChildren() do
  133.     Old[#Old + 1] = {Obj, Obj.Parent}
  134. end
  135.  
  136. for _, Obj in Storage:GetChildren() do
  137.     Old[#Old + 1] = {Obj, Obj.Parent}
  138. end
  139.  
  140. local function LoadData(Player)
  141.     local Success, Data = pcall(function()
  142.         return Datastore:GetAsync(Player.UserId)
  143.     end)
  144.    
  145.     if Success and Data then
  146.         return Data.Money or 0, Data.Buttons or {}
  147.     end
  148.  
  149.     return 0, {}
  150. end
  151.  
  152. local function SaveData(Player)
  153.     local OwnedButtons = {}
  154.     for _, Button in Buttons do
  155.         if Button[2].Parent == Tycoon then
  156.             OwnedButtons[#OwnedButtons + 1] = Button[3]
  157.         end
  158.     end
  159.    
  160.     local Data = {
  161.         Money = Player.leaderstats.Money.Value,
  162.         Buttons = OwnedButtons,
  163.     }
  164.    
  165.     pcall(function()
  166.         Datastore:SetAsync(Player.UserId, Data)
  167.     end)
  168. end
  169.  
  170. local function LoadStuff(Player)
  171.     local FoundMoney, FoundButtons = LoadData(Player)
  172.     Player.leaderstats.Money.Value = FoundMoney
  173.  
  174.     for _, ButtonCount in FoundButtons do
  175.         local Button = Buttons[ButtonCount]
  176.         Button[1].Parent = Storage
  177.         Button[2].Parent = Tycoon
  178.         Button[4]()
  179.  
  180.         local Next = ButtonCount + 1
  181.         local Found = Buttons[Next]
  182.         Found[1].Parent = Tycoon
  183.     end
  184. end
  185.  
  186. Players.PlayerAdded:Connect(function(Player)
  187.     local Leaderstats = Instance.new("Folder")
  188.     Leaderstats.Parent = Player
  189.     Leaderstats.Name = "leaderstats"
  190.  
  191.     local Money = Instance.new("IntValue")
  192.     Money.Parent = Leaderstats
  193.     Money.Name = "Money"
  194. end)
  195.  
  196. local function Unclaim()
  197.     if not isClaimed then return end
  198.     isClaimed = false
  199.     Head.Transparency = 0.5
  200.     Humanoid.DisplayName = "Claim"
  201.     Owner = nil
  202.  
  203.     for _, Data in Old do
  204.         Data[1].Parent = Data[2]
  205.     end
  206.  
  207.     Ores:ClearAllChildren()
  208. end
  209.  
  210. local function Claim(Target)
  211.     if isClaimed then return end
  212.    
  213.     isClaimed = true
  214.     Humanoid.DisplayName = ("%s's Tycoon"):format(Target.Name)
  215.     Head.Transparency = 0.8
  216.     Owner = Target
  217.     LoadStuff(Target)
  218.     PlayerLeft(Target, function()
  219.         SaveData(Target)
  220.         Unclaim()
  221.     end)
  222. end
  223.  
  224. PlayerTouch(Head, Claim)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement