Help needed for freeflow combat

So I’m currently attempting to make a freeflow combat system like the arkham and insomniac spiderman games. Overall help with this would be appreciated but my main concern is for it to be faster.

local function FindNearestEnemy()
	local nearestEnemy = nil
	local nearestDistance = math.huge

	for _, enemy in ipairs(workspace.Enemies:GetChildren()) do
		local distance = (enemy.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude
		if distance < nearestDistance then
			nearestEnemy = enemy
			nearestDistance = distance
		end
	end

	return nearestEnemy
end

local function MoveToEnemy()
	local enemy = FindNearestEnemy()

	if enemy then
		local path = game:GetService("PathfindingService"):FindPathAsync(Character.HumanoidRootPart.Position, enemy.HumanoidRootPart.Position)
		if path.Status == Enum.PathStatus.Success then
			local waypoints = path:GetWaypoints()

			for _, waypoint in ipairs(waypoints) do				
				
				Humanoid:MoveTo(waypoint.Position)
				Humanoid.MoveToFinished:Wait()
			end
		end
	end
end

UIS.InputBegan:Connect(function(i,GameProcessed)
	if GameProcessed then return end
	if i.UserInputType == Enum.UserInputType.MouseButton2 then
		wait(.2)
		MoveToEnemy()
	end
end)
1 Like