|
|
|
|
| local CollectionService = game:GetService("CollectionService")
|
| local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
| local Players = game:GetService("Players")
|
|
|
| local PlotConfig = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("PlotConfig"))
|
| local ClaimPlotEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("ClaimPlot")
|
|
|
|
|
| local activePlots = {}
|
|
|
| local function claimPlot(player, targetPlot)
|
|
|
| if not targetPlot or not targetPlot:IsA("BasePart") then return end
|
| if not CollectionService:HasTag(targetPlot, "EmptyPlot") then return end
|
|
|
|
|
| if activePlots[player.UserId] then
|
| print(player.Name .. " already owns a plot.")
|
| return
|
| end
|
|
|
|
|
| for _, ownedPlot in pairs(activePlots) do
|
| if ownedPlot == targetPlot then
|
| print("Plot already claimed.")
|
| return
|
| end
|
| end
|
|
|
|
|
| activePlots[player.UserId] = targetPlot
|
|
|
|
|
| targetPlot.BrickColor = BrickColor.new("Bright green")
|
| CollectionService:RemoveTag(targetPlot, "EmptyPlot")
|
| CollectionService:AddTag(targetPlot, "ClaimedPlot")
|
|
|
| targetPlot:SetAttribute("OwnerId", player.UserId)
|
|
|
| print(player.Name .. " successfully claimed a plot.")
|
|
|
|
|
| _G.LoadPlayerData(player, targetPlot)
|
| end
|
|
|
|
|
| ClaimPlotEvent.OnServerEvent:Connect(claimPlot)
|
|
|
|
|
| Players.PlayerRemoving:Connect(function(player)
|
| local plot = activePlots[player.UserId]
|
| if plot then
|
|
|
| _G.SavePlayerData(player, plot)
|
|
|
|
|
| plot.BrickColor = BrickColor.new("Dark stone grey")
|
| CollectionService:RemoveTag(plot, "ClaimedPlot")
|
| CollectionService:AddTag(plot, "EmptyPlot")
|
| plot:SetAttribute("OwnerId", nil)
|
|
|
|
|
| for _, obj in pairs(plot:GetChildren()) do
|
| if obj:IsA("Model") or obj:IsA("BasePart") then
|
| obj:Destroy()
|
| end
|
| end
|
|
|
| activePlots[player.UserId] = nil
|
| end
|
| end)
|
|
|