local Workspace = {}
setmetatable(Workspace, {
__index = workspace
})
function Workspace.new()
local NewWorkspace = {}
setmetatable(NewWorkspace, {
__index = Workspace
})
return NewWorkspace
end
function Workspace:GetObjectsOfClass(ClassType)
local Objects = {}
for _, Descendant in next, self.GetDescendants(workspace) do
if Descendant.ClassName ~= ClassType then continue end
table.insert(Objects, Descendant)
end
return Objects
end
local NewWorkspace = Workspace.new()
local Objects = NewWorkspace:GetObjectsOfClass("Part")
for _, Object in ipairs(Objects) do
print(Object.Name) --Baseplate
end
Unfortunately Lua doesnât support __super methods etc. to fetch the class/object a method was initially defined within meaning that the explict reference to workspace here self.GetDescendants(workspace) is necessary.
Didnât copy the entire script, Iâve edited the post, should be working now.
Hereâs the other thing you requested.
local Game = game
local Players = Game:GetService("Players")
local function OnPlayerAdded(Player)
local function OnCharacterAdded(Character)
local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid", 5)
if not Humanoid or (Humanoid and Humanoid.Health) <= 0 then return end
local HumanoidClass = {}
function HumanoidClass.new()
local HumanoidObject = {}
setmetatable(HumanoidObject, {
__index = HumanoidClass,
__newindex = Humanoid
})
return HumanoidObject
end
function HumanoidClass:SetSpeed(Speed)
self.WalkSpeed = Speed
end
local HumanoidWrapper = HumanoidClass.new() --Wrapper for 'real' humanoid object.
HumanoidWrapper:SetSpeed(50)
end
Player.CharacterAdded:Connect(OnCharacterAdded)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
Isnt that the same thing?
Why do we need to put workspace [the service] inside our original table, and put it again in our âmainâ functions function?
workspace is a global, declared by Roblox for all scripts (LuaSourceContainers), Workspace in this context is a declaration of a new table (that represents a class).
Also, it now gives me this little error: GetObjectsOfClass is not a valid member of Workspace "Workspace"
code:
local metatables = {}
setmetatable(metatables, {
__index = workspace
})
function metatables.new()
local NewWorkspace = {}
setmetatable(NewWorkspace, {
__index = workspace
})
return NewWorkspace
end
function metatables:GetObjectsOfClass(ClassType)
local Objects = {}
for _, Descendant in pairs(self.GetDescendants(workspace)) do
if Descendant.ClassName ~= ClassType then continue end
table.insert(Objects, Descendant)
end
return Objects
end
local Functions = metatables.new()
local Objects = Functions:GetObjectsOfClass("Part")
for _, Object in ipairs(Objects) do
print(Object.Name) --Baseplate
end
Yes, the initial table represents a class which holds variables/methods, a constructor function typically named ânewâ i.e; âClassName.new()â constructs objects of that class, the __index metamethod of those objects should point to the class itself, that way the objects inherit their classes variables/methods.
If you want those objects to interact with some Roblox object (act as wrappers for it) then youâll need to include additional __index and __newindex metamethods to allow for that behavior.