OOP Experimentation

Introduction

Hello, I decided that today I would experiment with OOP because I’ve had an interest in it for quite a while, after watching a video about it I decided to make my own OOP system.

I’ve never made abilities so I decided I would do that, and so here’s how the system works.

Essences - These are objects that players can inherit to gain powers.

Abilities - Each essence has abilities that players can use.

Users - This is a table that contains all essence users and their types.

Currently, the code works well, and the player gets the essence called Timesweeper but I want to make abilities for it. The issue with this is, that I’m not sure how I can check if a player clicks a specific key after they inherit an essence.

Note that the tool gets destroyed after the essence is claimed so that the player can equip a melee weapon.

CURRENT PROBLEM

You can check for a key press and within the function check if they’ve inherited the essence

But where should I do this and how should I prevent exploiters from exploiting it?

You can fire a remote event from client to server when the key is pressed and handle everything on the server so exploiters cant exploit it

Alright, I tried doing this but I ran into an issue. For some reason, even after I set “essences.users[player]” to a table, it returns as nil to the client, but after creating it on the server it showed me the table…

Essence Module

local essences = {} -- The full essence table, every essence can be inherited to gain abilities.

local abilities = {} -- These are the abilities that essences hold.

abilities.TimeSweeper = {
	backTrack = function(point1, point2, char) -- The way that backtrack works is it lets the user set up two points and then they can telaport betweent the two
		if not point1 then
			point1 = char.PrimaryPart.Position
			print("A")
		elseif not point2 then
			point2 = char.PrimaryPart.Position
			print("B")
		else
			print("C")
		end
	end,
}

setmetatable(abilities, essences) -- Essences is the parent class to Abilities

essences.users = {}
essences.users.essenceType = nil

setmetatable(essences.users, abilities) -- Abilities are the parent class to Users

essences.inheritEssence = function(player, essenceType) -- You can create users using this method
	essences.users[player] = { -- The player table that is created inside of the users table.
		abilityKeybind1 = {
			keyCode = Enum.KeyCode.Q,
			abilityType = nil,
			cooldown = false
		},

		abilityKeybind2 = {
			keyCode = Enum.KeyCode.E,
			abilityType = nil,
			cooldown = false
		},
		abilityKeybind3 = {
			keyCode = Enum.KeyCode.Z,
			abilityType = nil,
			cooldown = false
		},
		abilityKeybind4 = {
			keyCode = Enum.KeyCode.X,
			abilityType = nil,
			cooldown = false
		},
		abilityKeybind5 = {
			keyCode = Enum.KeyCode.C,
			abilityType = nil,
			cooldown = false
		},
	}
	
	print(essences.users[player])
	
	if essenceType then essences.users[player].essenceType = essenceType end -- Each essence tool can send a essenceType argument. This will set the user's essenceType to that.

	setmetatable(essences.users[player], essences.users) -- Users is the parent class to the players
	
	print(essences.users[player].essenceType) -- this simply prints the essence type of the user.

	return "Completed" -- returns completed to the essence tool
end

essences.useAbility = function(player, ability) -- the function that lets the player use abilities (not finished)
	local userEssence = essences.users[player].essenceType
	local ability = abilities[userEssence][ability]
end

essences.checkForEssence = function(player) -- the function that checks if the player has (a) essence(s).
	if essences.users[player] ~= nil then return essences.users[player].essenceType else return nil end
end

return essences

EssenceAbilities Local Script (StarterCharacterScripts)

--!strict

local player = game.Players.LocalPlayer

local replicatedStorage = game:GetService("ReplicatedStorage")
local uis = game:GetService("UserInputService")
local remotes = replicatedStorage:WaitForChild("Remotes")

local essenceAbilityRemote = remotes:WaitForChild("essenceAbility")
local essenceModule = require(replicatedStorage:WaitForChild("Essence"))

uis.InputBegan:Connect(function(inputObject, gpe)
	print(essenceModule.checkForEssence(player))
	if essenceModule.checkForEssence(player) ~= nil then -- checks if the player has an essence
		print(inputObject.KeyCode == essenceModule.users[player].abilityKeybind1.keyCode)
		if inputObject.KeyCode == essenceModule.users[player].abilityKeybind1.keyCode then
			print(essenceModule.users[player].abilityKeybind1.abilityType)
			if essenceModule.users[player].abilityKeybind1.abilityType ~= nil then
				essenceModule.useAbility(player, essenceModule.users[player].abilityKeybind1.abilityType)
			end
		end
	end
end)

I don’t think modules will share data between client and server