Just trying to optimize my code

i just need to get my code reviewed, because i want to learn how to make my games the most performant possible, by the way this is a combat system

ClientHandler

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Block = game.ReplicatedStorage.Remotes.Block
local Hit = game.ReplicatedStorage.Remotes.Hit
local PlayerHelper = require(game.ReplicatedStorage.Modules.PlayerHelper)

--UIS.InputBegan:Connect(function(Input, GameProcessed)
--	if Input.KeyCode == Enum.KeyCode.F then
--		Block:FireServer(Player)
--		print("Player started blocking")
--	end
--end)

--UIS.InputEnded:Connect(function(Input)
--	if Input.KeyCode == Enum.KeyCode.F then
--		print("Player stopped blocking")
--	end
--end)

UIS.InputBegan:Connect(function(Input, GameProcessed)
	local Character = Player.Character
	
	if not Character:GetAttribute("CanAttack") then return end
	if Character:FindFirstChildOfClass("Tool") then return end
	
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		Hit:FireServer(Player)
		print("Player is M1ing")
	end
end)

Hit.OnClientEvent:Connect(function(Combo)
	local Character = Player.Character
	local Humanoid = Character.Humanoid
	local RootPart = Character.PrimaryPart

	local AnimTrack = game.ReplicatedStorage.Animations[Character:GetAttribute("Stance")]:FindFirstChild("Hit"..Combo.Value)
	if not AnimTrack then
		print("Animation track not found:", "Hit"..Combo.Value)
		return
	end

	local Anim = Humanoid:LoadAnimation(AnimTrack)
	if not Anim then
		print("Failed to load animation")
		return
	end

	local Swing = Instance.new("Sound")
	Swing.SoundId = game.ReplicatedStorage.Swing.SoundId
	Swing.Volume = game.ReplicatedStorage.Swing.Volume
	Swing.Parent = RootPart
	
	local HitKeyframeTime = 0.2
	local HasPlayedSound = false

	Anim.KeyframeReached:Connect(function(KeyframeName)
		if KeyframeName == "Hit" and not HasPlayedSound then
			HasPlayedSound = true
			Swing.PlaybackSpeed = 0.9 + Combo.Value / 10
			Swing:Play()
			Swing.Ended:Connect(function()
				Swing:Destroy()
			end)
		end
	end)

	Anim:Play()

	Anim.Stopped:Connect(function()
		if Swing then
			Swing:Destroy()
		end
	end)	
end)

HitDetection (script)

local Block = game.ReplicatedStorage.Remotes.Block
local Hit = game.ReplicatedStorage.Remotes.Hit
local PlayerHelper = require(game.ReplicatedStorage.Modules.PlayerHelper)
local Debris = game:GetService("Debris")

Hit.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	if not Character:GetAttribute("CanAttack") then return end

	local Humanoid = Character.Humanoid
	local RootPart = Character.PrimaryPart

	local HitTarget = nil
	PlayerHelper.CreateHitbox(Player, RootPart.CFrame * Vector3.new(0, 0, -2), 0.6, function(Target)
		HitTarget = Target
	end)

	if Character.Combo.Value >= 3 then
		if HitTarget then
			PlayerHelper.Knockback(HitTarget, RootPart.CFrame.LookVector * 40)
		end
		Character.Combo.Value = 0
		Character:SetAttribute("CanAttack", false)
		task.wait(0.5)
		Character:SetAttribute("CanAttack", true)
	end

	Character.Combo.Value += 1
	Character:SetAttribute("CanAttack", false)
	Hit:FireClient(Player, Character.Combo)
	task.delay(0.84, function()
		Character:SetAttribute("CanAttack", true)
	end)
end)

PlayerHelper (module)

local Player = {}

local Debris = game:GetService("Debris")

function Player.CreateHitbox(Player, Position, Lifetime)
	local Character = Player.Character
	
	if not Character:GetAttribute("CanAttack") then return end
	
	local Humanoid = Character.Humanoid
	local RootPart = Character.PrimaryPart
	local Hitbox = game.ReplicatedStorage.Hitbox:Clone()
	Hitbox.Parent = RootPart
	Hitbox.Position = Position
	
	local Hit = workspace:GetPartsInPart(Hitbox)
	for i, v in pairs(Hit) do
		if v:FindFirstAncestorOfClass("Model") and v.Parent ~= Character and v.Name ~= "Hitbox" then
			task.delay(0.4, function()
				v.Parent.Humanoid:TakeDamage(3)
			end)
			break
		end
	end
	return Debris:AddItem(Hitbox, Lifetime)
end

function Player.Knockback(Target, Force)
	local RootPart = Target:FindFirstChild("HumanoidRootPart")
	if RootPart then
		local Humanoid = Target:FindFirstChild("Humanoid")
		if Humanoid then
			local BodyVelocity = Instance.new("BodyVelocity")
			BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			BodyVelocity.Velocity = Force
			BodyVelocity.Parent = RootPart
			game:GetService("Debris"):AddItem(BodyVelocity, 0.5)
		end
	end
end

return Player

Knockback doesnt work yet for some reason but i’m fixing it soon

and please also tell me how i can improve the code

1 Like

I recommend doing Hitbox from the client, you can prevent cheating by measuring player proximity from the server.
And I saw that you didn’t do the Humanoid check Check if there is a humanoid in the parent of the target side

2 Likes

thank you i just got done with the script i’m not gonna send everything all over again so i’ll explain it shortly i obviously check if the players input is mousebutton1 in my client handler then i create a hitbox with the function from the playerhelper which uses muchachohitbox, then i use muchachohitbox’s custom touched signal, do a few checks then i fire to server where i also do a check to see if the target humanoid is equal to the attackers humanoid then i make the target humanoid take damage

2 Likes

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