Voidmaster55

Tycoon Script

Jul 14th, 2024 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 KB | Source Code | 0 0
  1. local Plots = game.Workspace.Plots
  2. local TemplatePlot = game.Workspace.TemplatePlot
  3.  
  4. game.Players.PlayerAdded:Connect(function(player)
  5.     for _, plot in pairs(Plots:GetChildren()) do
  6.         if plot:GetAttribute('Taken') then
  7.             continue -- Skip to the next plot if this one is already taken
  8.         end
  9.  
  10.         plot:SetAttribute('Taken', true)
  11.         plot:SetAttribute('Owner', player.UserId)
  12.  
  13.         print('Plot has been given to ' .. player.Name .. '!')
  14.  
  15.         local ItemsFolder = Instance.new('Folder')
  16.         ItemsFolder.Name = 'Items'
  17.         ItemsFolder.Parent = plot
  18.  
  19.         -- Clone buttons from TemplatePlot and place them in the current plot
  20.         local TemplateButtons = TemplatePlot.Buttons:Clone()
  21.         local TemplateItems = TemplatePlot.Items
  22.  
  23.         for _, Button in pairs(TemplateButtons:GetChildren()) do -- Use pairs() for iterating over children
  24.  
  25.             local RelativeCFrame = TemplatePlot.CFrame:ToObjectSpace(Button.CFrame)
  26.             Button.CFrame = plot.CFrame:ToWorldSpace(RelativeCFrame)
  27.  
  28.             Button.Touched:Connect(function(hit)
  29.                 local touchedPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
  30.  
  31.                 if not touchedPlayer then return end
  32.                 if plot:GetAttribute('Owner') ~= touchedPlayer.UserId then return end
  33.  
  34.                 local itemToUnlockId = Button:GetAttribute('IdOfItemToUnlock')
  35.                 if not itemToUnlockId then
  36.                     warn('WARNING: You forgot to add an IdOfItemToUnlock attribute')
  37.                     return
  38.                 end
  39.  
  40.                 local Price = Button:GetAttribute('Price')
  41.                 if Price then
  42.                     if touchedPlayer.leaderstats.Cash.Value < Price then
  43.                         warn('You cannot afford this item!')
  44.                         return
  45.                     end
  46.                     touchedPlayer.leaderstats.Cash.Value = touchedPlayer.leaderstats.Cash.Value - Price
  47.                 end
  48.  
  49.                 for _, item in pairs(TemplateItems:GetChildren()) do -- Use pairs() for iterating over children
  50.                     if item:GetAttribute('Id') ~= itemToUnlockId then continue end
  51.  
  52.                     local itemClone = item:Clone()
  53.  
  54.                     local itemCFrame
  55.                     if itemClone:IsA('Model') then
  56.                         itemCFrame = itemClone:GetModelCFrame()
  57.                     elseif itemClone:IsA('BasePart') then
  58.                         itemCFrame = itemClone.CFrame
  59.                     end
  60.  
  61.                     if itemCFrame then
  62.                         local relativeItemCFrame = plot.CFrame:ToObjectSpace(itemCFrame)
  63.                         local worldCFrameOfNewPlot = plot.CFrame:ToWorldSpace(relativeItemCFrame)
  64.                         if itemClone:IsA('Model') then
  65.                             itemClone:PivotTo(worldCFrameOfNewPlot)
  66.                         elseif itemClone:IsA('BasePart') then
  67.                             itemClone.CFrame = worldCFrameOfNewPlot
  68.                         end
  69.                     end
  70.  
  71.                     for _, scriptObject in pairs(itemClone:GetDescendants()) do
  72.                         if scriptObject:IsA('BaseScript') then
  73.                             scriptObject.Disabled = false -- Enable scripts if they were disabled
  74.                         end
  75.                     end
  76.  
  77.                     itemClone.Parent = ItemsFolder
  78.                     Button:Destroy()
  79.                 end
  80.             end)
  81.  
  82.             Button.Parent = TemplateButtons
  83.         end
  84.  
  85.         TemplateButtons.Parent = plot
  86.         break -- Exit the loop once a plot is assigned
  87.     end
  88. end)
  89.  
  90. game.Players.PlayerRemoving:Connect(function(player)
  91.     for _, plot in pairs(Plots:GetChildren()) do
  92.         if not plot:GetAttribute('Owner') then
  93.             continue -- Skip plots without an owner attribute
  94.         end
  95.  
  96.         if plot:GetAttribute('Owner') ~= player.UserId then
  97.             continue -- Skip plots not owned by the leaving player
  98.         end
  99.  
  100.         plot:SetAttribute('Taken', nil)
  101.         plot:SetAttribute('Owner', nil)
  102.  
  103.         print('Plot has been removed from ' .. player.Name .. '!')
  104.         break -- Exit the loop once the plot is found and reset
  105.     end
  106. end)
  107.  
Advertisement
Add Comment
Please, Sign In to add comment