Custom Functions/Events

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.

Thank you!

It returns me this error:
GetObjectsOfClass is not a valid member of Workspace "Workspace" - Server - Test:21

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)

Thank you so much,

Tho, I was wondering -
image
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?

[The rest I understand]

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

This is weird, this one and the one you gave me are both identical with only different variables

function metatables.new()
	local NewWorkspace = {}
	setmetatable(NewWorkspace, {
		__index = workspace
	})
	return NewWorkspace
end

The ‘__index’ metamethod of the constructed object should point to the class itself, i.e; ‘metatables’, not ‘workspace’.

My bad, I indeed accidentally put there workspace instead of metatables, which seems to have fixed it.

So in conclusion,
the top table contains all our functions/events

And these lines insert workspace to that table because we need it

setmetatable(metatables, {
	__index = workspace
})

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.