Which way makes more sense?

I’m trying to rewrite some of my games code since a lot of it was somewhat rushed to quickly get a prototype/demo working. I’d like to spend more time now to focus on using composition being able to have mini classes to make up something.

This is my current player code.

function Player.new()
	local self = setmetatable({},Player)
	
	self.Animation = Animation.new(self)
	self.Input = Input.new(self)
	self._trove = Trove.new()
	self.canDoActions = true
	
	self._trove:Connect(localPlayer.CharacterAdded, function(char)
		self:OnCharacterAdded(char)
	end)
	
	self._trove:Connect(localPlayer.CharacterRemoving, function(char)
		self:OnCharacterRemoving(char)
	end)

	if localPlayer.Character then
		self:OnCharacterAdded(localPlayer.Character)
	end

	return self
end

This is the new version

function Player:Initialize()
	localPlayer = Players.LocalPlayer
	playerGui = localPlayer:WaitForChild("PlayerGui")
	screenGui = playerGui:WaitForChild("ScreenGui")
	
	localPlayer.CharacterAdded:Connect(function(char)
		OnCharacterAdded(char)
	end)

	localPlayer.CharacterRemoving:Connect(function(char)
		OnCharacterRemoving(char)
	end)

	if localPlayer.Character then
		OnCharacterAdded(localPlayer.Character)
	end
	
	-- Composition 
	-- The player is an entity that is damagable
	Player.entity = _G.Entity.new(character,100,16,7) --model, health, speed, jump
	Player.damagable = _G.Damagable.new(Player) -- Needed so the damage class can access our entity and change health
end

The only difference is I decided to ditch the OOP since IMO it seems unnecessary for a player module as I probably won’t be making multiple player objects.

1 Like

all the new code looks all ok to me.