Object Oriented Programming - Sword hit detection

Only just started learning OOP, watched a few tutorials and thought it would be a good idea to try to make something simple with OOP. I chose to create a sword with hit detection. I am very confused about what I should be doing to make this tool and its hit detection. Could anyone give any advice or help on how to make tools with OOP? My progress with the hit detection :

local class = require(game.ServerStorage.Modules.Classic)

local Melee = class:extend()

function Melee:activated(HitPart)
	self.WeaponHitbox = HitPart
	
	self.WeaponHitbox.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if player then
			print("Hit ", player)
		end
	end)
end

return Melee

So a class should have field and methods. For a melee weapon the the fields might be:

  • The part that is the sword
  • The amount of damage it does
  • Its owner

You then want your methods (functions) which are things the sword might do. A method to check if the sword hit something is fine.

If you wanted to get more technical you could make a base melee class and then have different types of melee weapons as classes that extend your base class.

Also if you want to learn a bit more about how classes work I would recommend writing them from scratch instead of using a module that creates them.

So your trying to detect if a player hit the sword?

Put in Script

local Sword = require(game.ServerStorage.ModuleScript)


local SwordClass = Sword.__init__("Sword", "Player")





local FoundPlayer = SwordClass:Swipe()


print(FoundPlayer)

put in module in server storage

local Sword = {}

Sword.__index = Sword

function Sword.__init__(Player, PlayerSword)
	return setmetatable({ 
		Sword = PlayerSword;
		Player = Player;
	}, Sword)
end

function Sword:Swipe()
	if not self.Sword then return end
	
	local Handle = self.Sword.Handle
	
	local Min = Handle.Position - (Handle.Size / 2)
	local Max = Handle.Position + (Handle.Size / 2)
	
	local RG3 = Region3.new(Min, Max)
	
	local Results = game.Workspace:FindPartsInRegion3WithIgnoreList(RG3, {self.Player.Character})
	
	for _, V in next, Results do
		if V.Parent:FindFirstchild("Humanoid") then
			print("Found Hmanoid")
			local Player = game.Players:GetPlayerFromCharacter(V.Parent)
			return Player
		end
	end
end

return Sword

I’d check out the raycastHitbox module, they’re using neat tricks with raycasting.

Rant below:

I am confused as to why the paradigm you chose (object-oriented-design) is relevant in this inquiry. For OOP to be OOP, it needs to do the following: encapsulation, abstraction, inheritance and polymorphism. To which a sword system doesn’t actually require. OOP has a significant impact on the amount of resources you use and the performance.

OOP is super well known, being top 3 most popular paradigm, which is used for architectural patterns such as layers and micro services, as well as whole languages are also revolved around it. They’re great for their purposes, but Roblox is already so inherently filled with it, just adding more OO-design where it doesn’t make sense just obfuscates your code.

Generally I wouldn’t apply OOP where it doesn’t make sense. If it doesn’t improve readability, don’t bother! In my opinion the code you find in the toolbox is just way nicer, in terms of how they handle such a system.

And now onto, a better example to exercise the paradigm, finite-state-machines! I used the concept of FSM when starting out with OOP to make Ais. Which well, soon later I’d discover the evil of OOP which is that it doesn’t give you the inheritance it promised you.