I’ll be going over very basic OOP and CollectionService concepts via. the creation of plots/properties. Side note, I won’t go too indepth on what OOP is because there’s many tutorials and videos on the subject [All about Object Oriented Programming] and if you’re interested you can take look.
Now, OOP stands for Object Oriented Programming, you may of heard of it or you may not have but it’s basically just a coding practice that uses tables to create objects and classes. For example, we could make a plot/property which would be considered an object.
In this example, we’ve created a module script called Properties and have made a function that creates a new property.
-- Properties Module Script
local Properties = {}
Properties.__index = Properties
function Properties.new(Property: Instance?, access: BasePart) -- Information we'll give the function
local self = setmetatable({}, Properties) -- Creation of object's metatable
self.Access = access -- access is the part that the player will touch to get ownership
self.Owner = nil -- The properties owner
self.Property = Property -- The property itself
self.Upgrades = {}
return self -- Returns the property object
end
return Properties
The code above is basically making an object just like how you’d add a part to the workspace, if we couldn’t visually see the part or its properties it’d be listed something like this,
PartInstance1 = {
Name = "Part",
Size = Vector3.new(5,5,5),
Orientation = Vector3.new(0,0,0).
...
}
Now just in this case were creating our own object each time the player claims a property
Here’s where some of the fun comes with OOP, did you notice how we have a bunch of stuff called “self”, well self is interesting because it allows us to change the properties of the property/plot from another function, here’s what I mean
You’ll notice the function Properties:GiveOwnership()
that we use a colon :
instead of a period .
basically the colon tells us that this function is connected to our property object, like how we apply a force to a part Basepart:ApplyImpulse()
– The basepart is the object and the ApplyImpulse is our connected function which changes the velocity property of the basepart. We’re doing the same but this time with the plots ownership property.
local Players = game:GetService("Players")
local Properties = {}
Properties.__index = Properties
function Properties.new(Property: Instance?, access: BasePart)
local self = setmetatable({}, Properties)
self.Access = access
self.Owner = nil
self.Property = Property
self.Upgrades = {}
return self
end
function Properties:GiveOwnership(player: Player)
self.Owner = player -- self.Owner is used to get the owner property from our object
return true
end
return Properties
We can even update tables for our objects, in this case the “self.Upgrade” is a table.
function Properties:AddUpgrade(Upgrade)
self.Upgrades[Upgrade] = true
return self.Upgrades
end
If you’re keeping up that’s great, if not that’s okay just take your time and maybe look at some other references especially the link I sent before. Hopefully, you can somewhat see why OOP is so powerful it allows us to manage multiple instances of objects each being unique and allowing us to change their properties.
So now we’ve created the perfect set up to mass produce plots. Let’s see how we actually run these functions now shall we.
Now in most cases we’d use another module for this set up but in this case we’ll just make a script in serverscriptservice. We’re going to be diving into collection service one of my favorite features on roblox by far for mass automation.
--Script
local CollectionService = game:GetService("CollectionService")
-- We'll also want to retrieve our properties module
local PropertyModule = require(game.ServerStorage:WaitForChild("Properties"))
-- A very important part is having a table for all the properties that are made
local PropertiesMade = {} -- This way we'll be able to actually use our plot objects
So before we go ahead let’s explain what specifically with CollectionService we’re gonna be using… and thats… tags. If you’ve ever used attributes on parts, objects, instances, etc. you’ll notice there’s a place for tags. If you haven’t used attributes, in the properties of most instances if you scroll down to the bottom you’ll see a section called “tags” and you’ll be able to add a tag.
We’ll be creating a tag named “Access” and putting this on the part we want the player to touch to obtain ownership of a property.
The reason CollectionService is so awesome is because now we can put this tag on any part and then we loop through all the tags to get all the parts with this specific tag.
Think of it like reset bricks, you give them the tag “reset” then you loop through all the reset tags that are connected to the bricks and we can put a touched event on every part with the reset tag and have the touched event reset the player.
-- Here's an example,
for _, brick in (CollectionService:GetTagged("reset")) do -- Loop through all the parts with the tag "reset"
brick.Touched:Connect(function(hit) -- Connect a touched event to them
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(100)
end
end
We’ll now be doing that with our access part,
--Script
local CollectionService = game:GetService("CollectionService")
local PropertyModule = require(game.ServerStorage:WaitForChild("Properties"))
local PropertiesMade = {}
-- Okay I know this may look scary but its not if we take this one step at a time
for _, accessPart in (CollectionService:GetTagged("Access")) do -- Loops through Access parts
accessPart.Touched:Connect(function(hit: BasePart) -- Connects a touch event to access parts
if hit and hit.Parent then -- if the part is touched
local char: Instance = hit.Parent
if char:FindFirstChild("Humanoid") then -- Check if part is a character
local player = Players:GetPlayerFromCharacter(char) -- get character's player
if not PropertiesMade[player] then -- Make sure player doesn't already have a property
local prop: Instance? = accessPart.Parent -- the accesspart's parent is the property folder
PropertiesMade[player] = PropertyModule.new(prop, accessPart) -- Adds the created object to the folder giving it to the specific player
PropertiesMade[player]:GiveOwnership(player) -- Gives ownership to the players created object
end
end
end
end)
end
This can seem kind of scary especially if this is your first time working with OOP or CS (CollectionService) but all we did was
– Get the AccessParts based off the “access” tag
– Allowed the AccessParts to be touchable
– If they were touched check if it was a player
– Checked if that player had a property by checking the PropertiesMade table for the player
– Added the player to the properties table with the creation of the plot if they didn’t have a property
– Gave the player ownership of the plot
Let’s explain what happened in these lines,
PropertiesMade[player] = PropertyModule.new(prop, accessPart)
PropertiesMade[player]:GiveOwnership(player)
Since I think these are the lines that may confuse you the most. The first line were creating our object from the module we made before, if you go back to the module we return “self” when we create a plot which is the object.
PropertiesMade[player]
This adds the Player to the table
PropertiesMade = {
Player1 =
}
Now we need to give that player something to equal’s in this case our object
PropertiesMade = {
Player1 = {
Access = access
Owner = nil
Property = Property
Upgrades = {}
}
}
Now we give the Player the ownership
PropertiesMade[player]:GiveOwnership(player)
PropertiesMade = {
Player1 = {
Access = access
Owner = Player1
Property = Property
Upgrades = {}
}
}
The reason why this line works is all because this is a object who now has functions remember that basepart:applyimpulse()
example I made well were doing the same thing now with the give ownership we don’t need to reference the function from the module script because the object already has the function attached to itself.
Now you have fully functioning plot objects, which get created when someone touches the accesspart.
I hope that you learned something new today from the post and if not my bad for wasting your time. If you have any critics, questions, or thoughts, I’ll respond to them.