Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DatastoreService = game:GetService("DataStoreService")
- local RStorage = game:GetService("ReplicatedStorage")
- local Players = game:GetService("Players")
- local Datastore = DatastoreService:GetDataStore("PlayersData")
- local Tycoon = script.Parent
- local Claim = Tycoon.Claim
- local Head = Claim.Head
- local Humanoid = Claim.Humanoid
- local isClaimed = false
- local Owner = nil
- local function PlayerTouch(Obj, Callback)
- Obj.Touched:Connect(function(Hit)
- if not Hit or not Hit.Parent or not Hit.Parent:FindFirstChildOfClass("Humanoid") then return end
- local Target = Players:GetPlayerFromCharacter(Hit.Parent)
- if not Target then return end
- Callback(Target)
- end)
- end
- local function PlayerLeft(Target, Callback)
- local Connection
- Connection = Players.PlayerRemoving:Connect(function(Player)
- if Player ~= Target then return end
- Callback()
- Connection:Disconnect()
- end)
- end
- local Storage = Instance.new("Folder")
- Storage.Parent = RStorage
- Storage.Name = "TycoonStorage"
- local Ores = Instance.new("Folder")
- Ores.Parent = workspace
- Ores.Name = "Ores"
- local function OwnsButton(Player, ButtonName)
- local Success, Owns = pcall(function()
- return Datastore:GetAsync(Player.UserId, ButtonName)
- end)
- if Success and Owns then
- return Owns
- end
- end
- local Buttons = {}
- local ButtonCount = 0
- local function OperateButton(Price, Button, Model, Callback)
- if #Buttons ~= 0 then
- Button.Parent.Parent = Storage
- end
- ButtonCount += 1
- local Next = ButtonCount + 1
- Buttons[ButtonCount] = {
- Button.Parent,
- Model,
- ButtonCount,
- Callback,
- }
- Model.Parent = Storage
- PlayerTouch(Button, function(Target)
- if not isClaimed or Owner ~= Target then return end
- if not Target or Target.leaderstats.Money.Value < Price then return end
- Button.Parent.Parent = Storage
- Model.Parent = Tycoon
- Owner.leaderstats.Money.Value -= Price
- Callback()
- local Found = Buttons[Next][1]
- if Found then
- Found.Parent = Tycoon
- end
- end)
- end
- local function CreateOre(Price, Origin)
- local Ore = Instance.new("Part")
- Ore.Parent = Ores
- Ore.Size = Vector3.new(1, 1, 1)
- Ore.CFrame = Origin.CFrame
- Ore:SetAttribute("Price", Price)
- end
- local DropperConveyor = Tycoon.DropperConveyor
- OperateButton(0, Tycoon.Start.Head, DropperConveyor, function()
- DropperConveyor.Conveyor.Velocity = Vector3.new(0, 0, 10)
- DropperConveyor.Furnace.Touched:Connect(function(Ore)
- if not Ore or Ore.Parent ~= Ores then return end
- local Price = Ore:GetAttribute("Price")
- Owner.leaderstats.Money.Value += Price
- Ore:Destroy()
- end)
- task.spawn(function()
- local DropPart = Tycoon.DropperConveyor.DropPart
- while task.wait(2) do
- if not isClaimed then break end
- CreateOre(1, DropPart)
- end
- end)
- end)
- OperateButton(15, Tycoon.UpgraderButton.Head, Tycoon.Upgrader, function()
- local Connection = Tycoon.Upgrader.UpgradePart.Touched:Connect(function(Ore)
- if not Ore or Ore.Parent ~= Ores then return end
- if Ore:GetAttribute("Upgraded") then return end
- Ore:SetAttribute("Upgraded", true)
- Ore:SetAttribute("Price", Ore:GetAttribute("Price") * 2)
- end)
- repeat task.wait() until not isClaimed
- Connection:Disconnect()
- end)
- local Old = {}
- for _, Obj in Tycoon:GetChildren() do
- Old[#Old + 1] = {Obj, Obj.Parent}
- end
- for _, Obj in Storage:GetChildren() do
- Old[#Old + 1] = {Obj, Obj.Parent}
- end
- local function LoadData(Player)
- local Success, Data = pcall(function()
- return Datastore:GetAsync(Player.UserId)
- end)
- if Success and Data then
- return Data.Money or 0, Data.Buttons or {}
- end
- return 0, {}
- end
- local function SaveData(Player)
- local OwnedButtons = {}
- for _, Button in Buttons do
- if Button[2].Parent == Tycoon then
- OwnedButtons[#OwnedButtons + 1] = Button[3]
- end
- end
- local Data = {
- Money = Player.leaderstats.Money.Value,
- Buttons = OwnedButtons,
- }
- pcall(function()
- Datastore:SetAsync(Player.UserId, Data)
- end)
- end
- local function LoadStuff(Player)
- local FoundMoney, FoundButtons = LoadData(Player)
- Player.leaderstats.Money.Value = FoundMoney
- for _, ButtonCount in FoundButtons do
- local Button = Buttons[ButtonCount]
- Button[1].Parent = Storage
- Button[2].Parent = Tycoon
- Button[4]()
- local Next = ButtonCount + 1
- local Found = Buttons[Next]
- Found[1].Parent = Tycoon
- end
- end
- Players.PlayerAdded:Connect(function(Player)
- local Leaderstats = Instance.new("Folder")
- Leaderstats.Parent = Player
- Leaderstats.Name = "leaderstats"
- local Money = Instance.new("IntValue")
- Money.Parent = Leaderstats
- Money.Name = "Money"
- end)
- local function Unclaim()
- if not isClaimed then return end
- isClaimed = false
- Head.Transparency = 0.5
- Humanoid.DisplayName = "Claim"
- Owner = nil
- for _, Data in Old do
- Data[1].Parent = Data[2]
- end
- Ores:ClearAllChildren()
- end
- local function Claim(Target)
- if isClaimed then return end
- isClaimed = true
- Humanoid.DisplayName = ("%s's Tycoon"):format(Target.Name)
- Head.Transparency = 0.8
- Owner = Target
- LoadStuff(Target)
- PlayerLeft(Target, function()
- SaveData(Target)
- Unclaim()
- end)
- end
- PlayerTouch(Head, Claim)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement