PlotService
Hey Developers! I recently made a Module to help with Managing Plots because I wanted to experiment with making Public Modules, and I decided to release it because why not!
API
First of all lets go into what this Module has to offer:
Documentation
Read Variables:
PlotService.Plots | Table | Returns all Existing Plots
Plot.Occupant | Player | Returns Player or nil
Plot.Occupied | Bool | Returns Bool
Plot.PlotObject | Instance | Returns Plot Object
Plot.PlotID | Int | Returns PlotID
Plot.Allowed | Table | Returns table with Allowed Players
Plot.Blacklisted | Table | Returns table with Blacklisted Players
==================================================================================================================
Events:
Plot.OccupantChanged:Connect(function(newOccupied , newOccupant) - Fires when Occupant is changed
NOTE: This does not fire when manually setting .Occupant and .Occupied!
==================================================================================================================
Constructor:
PlotService.new(plot) - Creates a new Plot with a PlotObject plot
==================================================================================================================
Methods:
Plot:Occupy(Player) - Sets Occupant of the Plot to Player
Plot:UnOccupy() - Sets Occupant of the Plot to nil (Resets Allowed & Blacklisted Tables)
Plot:Allow(Player) - Adds Player to Plot Allowed Table
Plot:UnAllow(Player) - Removes Player from Plot Allowed Table
Plot:Blacklist(Player) - Adds Player to Plot Blacklist Table
Plot:UnBlacklist(Player) - Removes Player from Plot Blacklist Table
PlotService.GetPlotByPlayer(Player) | Plot | Returns Plot with the Occupant Player or nil
PlotService.GetPlotByPlotObject(PlotObject) | Plot | Returns Plot with PlotObject PlotObject or nil
PlotService.GetPlayerByPlotObject(PlotObject) | Plot | Returns Occupant of Plot with PlotObject PlotObject or nil
PlotService.GetPlotByID(ID) | Plot | Returns Plot with a PlotID ID or nil
PlotService.GetActivePlots() | Table | Returns all Plots in use
PlotService.GetAvailablePlots() | Table | Returns all Plots not in use
Use Cases
This Module requires you to have some scripting experience in Order to use it, but it basically just lets you make a Plot System without much work required. It includes pretty much everything you need for basic Plots. An example script could be:
local PlotService = require(game:GetService("ReplicatedStorage").PlotService) -- require the Module
local PlotObjects = workspace.Plots -- Folder with Parts in them
for _,PlotObject in pairs(PlotObjects:GetChildren()) do -- Loop through all the Parts in the Folder and create a new Plot for it
Plot = PlotService.new(PlotObject)
PlotObject.Name = Plot.PlotID -- Set the Name of the Part to the Plots ID (just for looks)
end
for _,Plot in pairs(PlotService.Plots) do -- loop through all Plots
Plot.PlotObject.Touched:Connect(function(hit) -- check if someone touches it
local Player = game.Players:GetPlayerFromCharacter(hit.Parent) -- get the Player that touched it
if (Player and not Plot.Occupied) then
local ExistingPlot = PlotService.GetPlotByPlayer(Player) -- check if the Player already has a Plot
if (ExistingPlot and ExistingPlot ~= Plot and ExistingPlot.Occupied) then -- if they have one then Reset it
ExistingPlot.PlotObject.Name = ExistingPlot.PlotID
ExistingPlot.PlotObject.Color = Color3.fromRGB(255,255,255)
ExistingPlot:UnOccupy()
end
Plot:Occupy(Player) -- Give them the Plot they touched
Plot.PlotObject.Name = Plot.Occupant.Name
Plot.PlotObject.Color = Color3.fromRGB(50,50,50)
end
end)
Plot.OccupantChanged:Connect(function(newOccupied,newOccupant)
print(newOccupied,newOccupant) -- Prints whenever the Plots Occupant is changed
end)
end
You could also make a script randomly assign a Plot when a Player joins using math.random
and PlotService.GetAvailablePlots()
together with PlotService.Plots
to get a random Plot!
Link
You can get the Module here: PlotService - Roblox
End Note
Thank you for reading this, remember that this is my first Public Module so any feedback would be appreciated. Thanks!