How does composition work?

I’m planning to make AI that can basically do anything the player can do the only obvious difference is it’s not controlled by a player instead I’m going to use a behavior tree and I wanna use composition to do this neatly.

My current plan is going to be to make 3 classes (module scripts) a character class that will set up everything they have in common methods, states, health, walk speed, etc. Then a player class will probably just handle input, and then the AI class which will control the behavior tree.

The issue though is I don’t think this is how composition works so what would be the proper way to go about doing this?

Edit: if I’m understanding this correctly essentially what I need to do is isolate the needed behavior into its own class and then add then I can just add that behavior to any class that needs it?

yes you are doing it right, you just stick stuff inside other stuff and redo the functions on the outside if needed. I like to use ._Inner to reference the main component.

local Entity = {}
-- ...
function Entity:Attack(dmg: number) end
-- ...

local Player = {}
Player.__index = Player
function Player.new(hp: number)
    local self = setmetatable({}, Player)
    self._Inner = Entity.new(hp)
    return self
end

function Player:Attack()
    self._Inner:Attack(1337)
end

local Zombie = {}
-- same as above

function Zombie:Attack()
    self._Inner:Attack(888)
end