Need help with OOP

hi im new to using OOP, im stuck on a part

i dont know how to explain this

i want to input data into the recordedmatch in the self.RecordedMatch
the data is called from the client to the server to the module so how can i access the self.RecordedMatch? is there a way to find this specific self?

(module script)

function ChessModule.new(Board : Model)

	local self = {
		Board = Board,
		BoardParts = Board:WaitForChild("BoardParts"):GetChildren(),
		BoardPieces = Board:WaitForChild("Pieces"),
		RecordedMatch = {},
		LegalMovesOnBoard = {}
	}
	
	setmetatable(self, ChessModule)
	
	self:Intialize()
	
	return self
end

(server script)

MoveEvent.OnServerEvent:Connect(function(Player, ChessModel, Piece, Square)
	
	ChessModule:MoveEventHandler(Player, ChessModel, Piece, Square)
	
end)

i cant find a way to access the self.object

You don’t understand why OOP is used, you have to create an object. You’re using the : notation to call a method on the class which kinda defeats the purpose. Instead, create an object of the class using the method you created ChessModule.new(), and access the RecordedMatch field using .RecordedMatch

local object = ChessModule.new(...)

object.RecordedMatch -- you access it here
1 Like

oh okay,
so inputting data like this would have to be done from a server/client script?

To input the data into that specific object, you would have to have access to that object in the script, meaning be in its scope.

1 Like

If you really wanted to pass Objects across the network you would either have to implement your own networking layer for reinstantiating objects since metatables are not preserved across the client-server boundary

Or…

-- You could do what some of the built-in libraries do (like buffer or vector)
-- and have the methods on a separate table like this for example:

local ChessModule = {}

function ChessModule.new()
	local self = {
		Board = Board,
		BoardParts = Board:WaitForChild("BoardParts"):GetChildren(),
		BoardPieces = Board:WaitForChild("Pieces"),
		RecordedMatch = {},
		LegalMovesOnBoard = {}
	}
	
	return self
end

function ChessModule.Initialize(self)
	-- Code here
end

function ChessModule.MoveEventHandler(self, Player, ChessModel, Piece, Square)
	-- Code here
end

Then you wouldn’t have to recreate the same object after passing it through a remote event!

-- You could then run your code like this:
local NewChessBoard = ChessModule.new(...)

ChessModule.Initialize(NewChessBoard)
ChessModule.MoveEventHandler(NewChessBoard, ...)
1 Like

thanks for responding , this is confusing to me

ive set up a new way and it seems to be working but i’d like to know if its an efficient method

theres gonna be multiple chess boards at once

im using one server script to host all games with the CreateGame() function ; i created 2 games for example

(server script)


function CreateGame(Board)
	local LoadBoard = ChessModule.new(Board)
	
	MoveEvent.OnServerEvent:Connect(function(Player, ChessModel, Piece, Square)
		if LoadBoard.Board ~= ChessModel then return end
		ChessModule:MoveEventHandler(Player, ChessModel, Piece, Square, LoadBoard)
		print(LoadBoard, "here")

	end)
	
end

CreateGame(workspace["Chess board"])
CreateGame(workspace["Chess board2"])

(module script)

function ChessModule:MoveEventHandler(Player, ChessModel, Piece, Square, SelfObject)

		
if IsPieceOnMove then
SelfObject.RecordedMatch[#SelfObject.RecordedMatch +1] = IsPieceOnMove
	
end

this is my first time trying to use OOP properly; is what i’ve done efficient?

You don’t need to put the colon in the function definition and the function call:

function CreateGame(Board)
	local LoadBoard = ChessModule.new(Board)
	
	MoveEvent.OnServerEvent:Connect(function(Player, ChessModel, Piece, Square)
		if LoadBoard.Board ~= ChessModel then return end
		ChessModule.MoveEventHandler(Player, ChessModel, Piece, Square, LoadBoard)
		print(LoadBoard, "here")

	end)
	
end

CreateGame(workspace["Chess board"])
CreateGame(workspace["Chess board2"])
function ChessModule.MoveEventHandler(Player, ChessModel, Piece, Square, SelfObject)

		
	if IsPieceOnMove then
		SelfObject.RecordedMatch[#SelfObject.RecordedMatch +1] = IsPieceOnMove
	end
end

If your not going to use the “self” keyword in a function don’t initialize the function with “:”

Other than that it looks fine to me

1 Like

I didnt know that, thank u alot for your help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.