Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Plots = game.Workspace.Plots
- local TemplatePlot = game.Workspace.TemplatePlot
- game.Players.PlayerAdded:Connect(function(player)
- for _, plot in pairs(Plots:GetChildren()) do
- if plot:GetAttribute('Taken') then
- continue -- Skip to the next plot if this one is already taken
- end
- plot:SetAttribute('Taken', true)
- plot:SetAttribute('Owner', player.UserId)
- print('Plot has been given to ' .. player.Name .. '!')
- local ItemsFolder = Instance.new('Folder')
- ItemsFolder.Name = 'Items'
- ItemsFolder.Parent = plot
- -- Clone buttons from TemplatePlot and place them in the current plot
- local TemplateButtons = TemplatePlot.Buttons:Clone()
- local TemplateItems = TemplatePlot.Items
- for _, Button in pairs(TemplateButtons:GetChildren()) do -- Use pairs() for iterating over children
- local RelativeCFrame = TemplatePlot.CFrame:ToObjectSpace(Button.CFrame)
- Button.CFrame = plot.CFrame:ToWorldSpace(RelativeCFrame)
- Button.Touched:Connect(function(hit)
- local touchedPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
- if not touchedPlayer then return end
- if plot:GetAttribute('Owner') ~= touchedPlayer.UserId then return end
- local itemToUnlockId = Button:GetAttribute('IdOfItemToUnlock')
- if not itemToUnlockId then
- warn('WARNING: You forgot to add an IdOfItemToUnlock attribute')
- return
- end
- local Price = Button:GetAttribute('Price')
- if Price then
- if touchedPlayer.leaderstats.Cash.Value < Price then
- warn('You cannot afford this item!')
- return
- end
- touchedPlayer.leaderstats.Cash.Value = touchedPlayer.leaderstats.Cash.Value - Price
- end
- for _, item in pairs(TemplateItems:GetChildren()) do -- Use pairs() for iterating over children
- if item:GetAttribute('Id') ~= itemToUnlockId then continue end
- local itemClone = item:Clone()
- local itemCFrame
- if itemClone:IsA('Model') then
- itemCFrame = itemClone:GetModelCFrame()
- elseif itemClone:IsA('BasePart') then
- itemCFrame = itemClone.CFrame
- end
- if itemCFrame then
- local relativeItemCFrame = plot.CFrame:ToObjectSpace(itemCFrame)
- local worldCFrameOfNewPlot = plot.CFrame:ToWorldSpace(relativeItemCFrame)
- if itemClone:IsA('Model') then
- itemClone:PivotTo(worldCFrameOfNewPlot)
- elseif itemClone:IsA('BasePart') then
- itemClone.CFrame = worldCFrameOfNewPlot
- end
- end
- for _, scriptObject in pairs(itemClone:GetDescendants()) do
- if scriptObject:IsA('BaseScript') then
- scriptObject.Disabled = false -- Enable scripts if they were disabled
- end
- end
- itemClone.Parent = ItemsFolder
- Button:Destroy()
- end
- end)
- Button.Parent = TemplateButtons
- end
- TemplateButtons.Parent = plot
- break -- Exit the loop once a plot is assigned
- end
- end)
- game.Players.PlayerRemoving:Connect(function(player)
- for _, plot in pairs(Plots:GetChildren()) do
- if not plot:GetAttribute('Owner') then
- continue -- Skip plots without an owner attribute
- end
- if plot:GetAttribute('Owner') ~= player.UserId then
- continue -- Skip plots not owned by the leaving player
- end
- plot:SetAttribute('Taken', nil)
- plot:SetAttribute('Owner', nil)
- print('Plot has been removed from ' .. player.Name .. '!')
- break -- Exit the loop once the plot is found and reset
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment