Please Review my combat system!

I wanted to know, if my code was right, I used something new for me the callback function!

  • I have 6 months experience
  • I am 15 years old and I am French
  • Thank you for your opinion on my code!
  • I’m not the PC to make videos to show you an overview
--Local script
--//Service Variable
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
--//Module Variable
local Red = require(ReplicatedStorage.Modules.Red)
local MainModule = require(ReplicatedStorage.Modules.MainModule)
local remoteEvent = Red.Client("Events")
--//Player Variable
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character.HumanoidRootPart
local humanoid = character.Humanoid :: Humanoid
--//Value Varaible
local newIndex = 1
local cooldown = true
local damage = 10  
local newHit
--//Array Variable
local colliderArray = {
	[1] = {
		["AnimationId"] = "rbxassetid://0000",
		["BasePart"] = "Right Arm",
	},
	[2] = {
		["AnimationId"] = "rbxassetid://0000",
		["BasePart"] = "Left Arm",
	},
	[3] = {
		["AnimationId"] = "rbxassetid://0000",
		["BasePart"] = "Right Leg",
	},
}
--//Typage
type RigTouchInfo = {Character: Model, Humanoid: Humanoid, Damage: number}
--//Fonctions
local function onNewIndex()
	if newIndex >= #colliderArray then
		newIndex = 1
	else
		newIndex += 1
	end
end

local function onInputBegan(input)
	local value = colliderArray[newIndex]
	local rigTouchArray = {} :: RigTouchInfo
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if cooldown then
			local animationTrack = MainModule.Combat:PlayAnimation(value.AnimationId)
			local basePart = character:FindFirstChild(value.BasePart)
			cooldown = false
			if newHit then
				newHit.CanTouch = true
			end
			MainModule.Combat:TouchedRig(basePart, function(hit)
				rigTouchArray = {Character = hit.Parent, Humanoid = hit.Parent.Humanoid, Damage = damage}
				remoteEvent:Fire("TakesDamageEvent", rigTouchArray)
				newHit = hit
			end)
			animationTrack.Stopped:Wait()
			MainModule.Combat:AnimationEnded(animationTrack, function()
				cooldown = true
				onNewIndex()
			end)
		end
	end
end

UserInputService.InputBegan:Connect(onInputBegan)
--Server script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Red = ReplicatedStorage.Modules.Red

local ok, result = pcall(require, Red)

local function KnockBack(pHRP, rHrp)
	local Speed = 50
	local Force = 10900

	local TotalForce = Force

	local KnockBack = Instance.new("BodyVelocity")
	KnockBack.Parent = rHrp

	KnockBack.MaxForce = Vector3.new(TotalForce, TotalForce, TotalForce)
	KnockBack.Velocity = pHRP.CFrame.LookVector * Speed
	task.wait(0.2)
	KnockBack.Parent = nil
end

if ok and result.Loader then
	local remoteEvent = result.Server("Events")
	
	remoteEvent:On("TakesDamageEvent", function(player: Player, rig: {})
		local health = rig.Humanoid.MaxHealth
		KnockBack(player.Character.HumanoidRootPart, rig.Character.HumanoidRootPart)
		rig.Humanoid:TakeDamage(rig.Damage)
		if health ~= rig.Humanoid.Health then
			print(rig.Humanoid.Health)
		end
	end)
else
	warn(`Failed loading {Red.Name}`)
end
--Main Module
local module = {}

local function loader(...)
	return
end

return {
	Loader = loader(),
	Combat = require(script.Combat),
}
--Combat Module
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid :: Humanoid

local module = {}

function module:PlayAnimation(animationId)
	local animation = Instance.new("Animation")
	animation.AnimationId = animationId
	local animationTrack = humanoid:LoadAnimation(animation)
	animationTrack:Play()
	return animationTrack
end

function module:TouchedRig(basePart: BasePart, callBack: any?)
	basePart.Touched:Connect(function(hit)
		if hit.Name == "HumanoidRootPart" then
			if not hit.CanTouch then return end
			hit.CanTouch = false
			return callBack(hit)
		end
	end)
end

function module:AnimationEnded(animationTrack, callBack: any?)
	animationTrack.Ended:Connect(function()
		return callBack()
	end)
end

return module
``` edit: "I add Animetrack.Stopped:Wait"
1 Like

no need for this because if there is no index given this is automatically put as “1” and etc for the 2 and 3 fyi, so it can be written like this:

local colliderArray = {
	{
		["AnimationId"] = "rbxassetid://0000",
		["BasePart"] = "Right Arm",
	},
	{
		["AnimationId"] = "rbxassetid://0000",
		["BasePart"] = "Left Arm",
	},
	{
		["AnimationId"] = "rbxassetid://0000",
		["BasePart"] = "Right Leg",
	},
}
3 Likes